Camera Movement with Character - AS3

90 views Asked by At

So I'm creating a simple 2d game in the vein of bitrunner, with character moving to the right. And I'm having trouble getting the camera to stick to the character.

I'm very much in the beginning of learning as3 and have never done this kind of thing before. I've looked up for examples of this, but so far I haven't found any simple code that I understood enough to put into my game. And quite a few say it is so simple, that I shouldn't need code to do it...but I do.

I'm better at learning from code I can see, than someone explaining it to me.

//stop/start animations
Player.stop();
rocket_1.play();

//variables
var gravity = 5;
var speedy = 20;
var speedx = 5;

//event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN,charMove);
stage.addEventListener(KeyboardEvent.KEY_UP, stand);
stage.addEventListener(Event.ENTER_FRAME, grav);

function grav(event:Event): void{
    Player.y += gravity;
}


//functions
function charMove(event:KeyboardEvent): void{
    switch (event.keyCode){

        case Keyboard.RIGHT:
        trace("I've been pressed right");
        Player.x += speedx;
        Player.play();
        break;

        case Keyboard.SPACE:
        trace("space");
        Player.y -= speedy;
        break;

    }
}

function stand(event:KeyboardEvent): void{
    Player.gotoAndStop(1);
}
1

There are 1 answers

1
Pedro Martins On

In Flash there is no default Camera, there is a stage, with a size, width and height, so if you character move outside the stage, you can“t see it anymore.

so you have to make everything move on x axis, but not your character..