HaxeFlixel Tutorial – Multiple Cameras

If you check the flixel-demos Github page, you can find sample code on how to implement split screen camera, as shown on the demo page.

In this tutorial, we will create a simple platformer which implements 4 cameras. The purpose is to demonstrate that it is possible to have multiple cameras with different properties.

Setup

As usual, let’s create a new template HaxeFlixel project from the terminal:

We won’t be needing any assets, because we’ll use the default HaxeFlixel graphics.

Code

First, let’s create a map:

And here’s the result when you build it with lime test neko :

Screen Shot 2015-04-11 at 10.31.30 AM

And now let’s add a playable character, complete with collision and movement logic:

And here’s how it’ll look:

licecap-camera2

Although it isn’t shown, the camera takes up the full area of the screen (in the case above, it’s 640 x 480). We can resize the cameras to only fit a portion of the screen — and along with this, we can create multiple cameras, to achieve split-screen multiplayer effects. Let’s add some code to make this happen:

Note: The FlxG has a variable camera  that controls the first main camera, whereas the cameras  variable holds an list of all the cameras. Be careful not to make typos here.

Now when you test, you’ll get this:

licecap-camera2-2

Notice that the background of each camera is of a different colour, but the screen is still showing the same content (one red player moving around). What this means to us, is that we know each camera has properties that can be customised individually.

Now, by knowing this, we can expand on the code to do the following:

When you test, you will see that this is starting to look like a 4-player split-screen game:

licecap-camera2-3

Please excuse the erratic movements — I was mashing my keyboard to simulate 4 players moving around in their individual screens.

Anyway, I hope this tutorial gives you an idea of how multiple cameras can be used (e.g. for mini-maps or split-screen multiplayers). You can refer to the FlxCamera API for more info. This concludes the tutorial on creating multiple cameras in HaxeFlixel.

Leave a Reply

Your email address will not be published. Required fields are marked *