thor625
thor625

Reputation: 87

Camera movement in AS3

OK so i have a character that moves with the mouse. I need it to stay in the center of the screen(kind of like a platformer game). I can't figure out how to access the camera and move it. (Note: I have tried Vcam and moving all of the other objects but Vcam makes the file slow or something [or so i have heard] and moving the other objects in kind of like cheating [and for my needs is insufficient]) I don't have any code because i don't know where to start. Maybe someone can point me into the right direction.

Thanks, Thor

Upvotes: 0

Views: 4599

Answers (3)

Dr.Denis McCracleJizz
Dr.Denis McCracleJizz

Reputation: 870

Your camera is only a vector that modifies position of all renderable objects.

myMovieClip.x = movingClipPosition.x + camera.x

So if the camera.x is moved to the right, this will make the object move the left, giving the impression of a "camera".

Upvotes: 0

Marty
Marty

Reputation: 39458

This is my previous answer on a similar question found here.

What I do here is:

Create a Map class with a property camera which is another custom class MapCamera.

The MapCamera has five properties:

  1. _x
  2. _y
  3. map - a reference to the instance of Map owning this MapCamera
  4. offsetX
  5. offsetY

    • The offset values represent the x and y spacing from the left and top edges of the screen, which should be set to half of the stage width and height so that the camera will centre on the stage correctly.
    • The _x and _y properties are private, and have getters and setters.

The getters are pretty basic:

public function get x():Number{ return _x; }
public function get y():Number{ return _y; }

The setters are where the viewport will be altered, like so:

public function set x(n:Number):void
{
    _x = n;
    map.x = -(_x + offsetX);
}

public function set y(n:Number):void
{
    _y = n;
    map.y = -(_y + offsetY);
}

From here, you add your children into the Map container and then can simply go:

map.camera.x = player.x;
map.camera.y = player.y;

Which will cause the player to always be in the centre of the screen.

Upvotes: 1

M. Laing
M. Laing

Reputation: 1617

One way is to store everyhting in one DisplayObject and then move that single object based on the camera movement. Instead of moving the camera, move the main container the opposite direction of the camera. I'm not sure why you seem to suggest a strategy like this is "cheating" as it is a perfectly suitable way to doing this.

Upvotes: 1

Related Questions