Skip navigation

Game developers often wants to keep an entity (usually player controlled entity) in their game to be always at the center of the screen. Imagine you are playing Mario Bros. Wherever you move mario, he will always be at the center of the screen. The background is moving to keep mario’s location presistency.

Today, I made a class to do just that easily. Before I explain the usage and technical details, you may want to see the demo first (pardon the artistic side of the demo ;)). So here it is :

View the Demo of Game Camera Class.

This class is actually very straightforward. You need to instantiate it then call .to(); to do the pan and zoom.
Here is the contructor :

public function GCam(canvas:Sprite, bound:Rectangle)

This class takes 2 arguments to construct, they are :
canvas – Sprite that serve as container for all of your game entities
bounds – a rectangle object that define the boundary of your world

This class takes boundary to know how much it is allowed to pan. This is very useful, because you always want to limit the panning of the camera so that it will not expose area outside your stage.

This class only has one function :

public function to(x:Number, y:Number, zoomAmount:Number, duration:Number):void;

This function takes 4 arguments, those are :
x – the x position of point that will be at the center of the screen
y – the y position of point that will be at the center of the screen
zoomAmount – how much will the camera zoom (think of it like scaleX and scaleY in sprite)
duration – how long will it take to do the zoom and panning

This function can be used neatly to center any of your game entity like this :, entity.y, 1.0, 1.0);

You may notice that, Yes, it does tween to do the panning and zooming. So you will get a smooth panning and zooming. However, you may pass 0 to the duration to make it choppy.

This class depends on open source tweening engine TweenLite to do the tweening. Make sure you have it before you use this class. If you don’t have it already, you can click here download TweenLite.

Well, that’s all, actually. Feel free to download Game Camera Class.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: