View Full Version : [AS3] Game Design Question

09-15-2009, 07:02 PM
I am trying to design a game where the Hero is always positioned in the middle of the screen.

If he walks right, the world moves to the left instead of the character stepping to the right.

Now my question is... What is the best way to implement this?

I have a World object, a Hero object, and several Enemy objects.

What would you attach to what, and how would your "movement" functions look?

The only part of the Hero that "moves" is its rotation. It's placement on the screen should always remain in the center.

09-15-2009, 07:46 PM
Check out tonyPA's tile based tutorials. I believe they are all in As2, but the concepts are the same regardless.

He may not specifically deal with games where the character stays centered, but it shouldn't really matter. Either way the background is scrolling, you just don't need the evaluation of the player position.


09-19-2009, 03:16 PM
The way I did this in my game was to put the whole game in an object container (mc_world) and then move that container in the opposite direction of the hero (while making sure you shift it relative to the "screen"). Just make sure that everything in your game is in this container (including your hero). I made a seperate CameraController class because I had other functions in there. But basically this is the function I used to center the "camera" around an object:

// Change these to match your screen size:
public const SCREEN_WIDTH = 550;
public const SCREEN_HEIGHT = 400;

// Call this function every frame. mc_object is the object you want to center on.
// mc_world is the container of the entire game.
public function lookAt(mc_object,mc_world):void{
mc_world.x = SCREEN_WIDTH/2.0 - mc_object.x;
mc_world.y = SCREEN_HEIGHT/2.0 - mc_object.y;

// Sample main loop:
function mainLoop(e:Event):void{
// ...
// ...

NOTE: I had to change the lookAt function to simplify it for this case (because I used Box2D in my game). So I'm not sure if it works 100% now that I changed it. But it did work in my game.

You then don't have to worry about individual entities (enemies, etc). You move your hero like you normally would. Since the hero is in mc_world, he will move relative to that world. But lookAt moves the entire world so that the hero is centered (or any other object you pass it).

Hope that helps. Good luck.

PS. I'm not sure if this is the best or most efficient way of doing things. This is just the way I did it and it worked fine for me.