Making a camera class

I’ve been using haxe and am wondering how I can go about making a camera class. Looking through some source code on github, I think creating a large list of entities in the world and translate them based on on the player. Something like

for(Entity in world) {
    all the entities set position -player position when a certain key is pressed
}

Although I couldn’t find the time to pry through the code of anyone or make much sense of some of it, how have you implemented camera functionality in openfl? There aren’t any tutorials or guides on it i have found so i’d appreciate a similar tutorial or some code to get started.

I managed to simulate a 2D camera by placing all of the entities normally on the Sprite (on their world positions), and then translating the Sprite inside the stage. So when the upper-left part of the camera is at (20, 20), you place the upper left of the Sprite at (20, 20).

You can also scale and rotate the Sprite, if you want to do camera rotation and zooming.

Using a Sprite will save you all kinds of headaches later on.

class World extends Sprite {
    public function new() {
        super();
    }
    
    public function addEntity(entity:DisplayObject):Void {
        addChild(entity);
    }
    
    public function centerCamera(cameraX:Float, cameraY:Float):Void {
        x = -cameraX + stage.stageWidth / 2;
        y = -cameraY + stage.stageHeight / 2;
    }
}
1 Like

Is is possible to define sprite bounds (ie camera boundaries) that are smaller than the screen width and height so that part of the camera sprite that are out of this bounds are not drawn? Because I want to have a camera sprite where I can zoom and translate but with a header and footer strip, above and below, so as a background, that are fixed.

I assume you already considered putting the header and footer in front of the sprite. If that isn’t enough, try scrollRect.