Hamza
Hamza

Reputation: 630

Character jumping while moving right and left AS3

I am making a platform game where the main character moves around and jumps.

I want the character to jump left and right separately. Maybe using two keys at the same time And land on top of the floor. My characters movie clip symbol is Naruto and my floor movie clip symbol is floor.

My project file can be found here: Naruto Game

In order to do this I have a main movie-clip with all the other movie-clips inside such as "jump right" and "jump left".

What I'm having a problem with, is when THE USER IS MOVING RIGHT I WANT THE CHARACTER TO FACE RIGHT WHEN JUMPING (and the same with the left).

My stage

 import flash.events.KeyboardEvent;
 import flash.ui.Keyboard;
 import flash.display.MovieClip;
 import flash.events.Event;
 import flash.display.Stage;

 naruto.gotoAndStop("stance");
 var rightPressed: Boolean = new Boolean(false);
 var leftPressed: Boolean = new Boolean(false);
 var upPressed: Boolean = new Boolean(false);
 var downPressed: Boolean = new Boolean(false);
 var narutoSpeed: Number = 10;
 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
 stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
 stage.addEventListener(Event.ENTER_FRAME, gameLoop);


 function keyDownHandler(keyEvent: KeyboardEvent): void {
    if (keyEvent.keyCode == Keyboard.RIGHT) {
        rightPressed = true;
    } else if (keyEvent.keyCode == Keyboard.LEFT) {
        leftPressed = true;
    } else if (keyEvent.keyCode == Keyboard.UP) {
        upPressed = true;
    } else if (keyEvent.keyCode == Keyboard.DOWN) {
        downPressed = true;
    }

 }

 function keyUpHandler(keyEvent: KeyboardEvent): void {
    if (keyEvent.keyCode == Keyboard.RIGHT) {
        rightPressed = false;
        naruto.gotoAndStop("standright")
    } else if (keyEvent.keyCode == Keyboard.LEFT) {
        leftPressed = false;
        naruto.gotoAndStop("standleft")
    } else if (keyEvent.keyCode == Keyboard.UP) {
        upPressed = false;
        naruto.gotoAndStop("stance")
    } else if (keyEvent.keyCode == Keyboard.DOWN) {
        downPressed = false;
        naruto.gotoAndStop("stance")
    }

 }

 function gameLoop(loopEvent: Event): void {
    if (rightPressed) {
        naruto.x += narutoSpeed;
        naruto.gotoAndStop("right");


    } else if (leftPressed) {
        naruto.x -= narutoSpeed;
        naruto.gotoAndStop("left");

    } else if (upPressed) {

        naruto.gotoAndStop("jumpright");

    }

 }

I owe so much to the person that can solve this I have been trying to solve this for a week! Thank you very much!

Upvotes: 1

Views: 483

Answers (1)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

To solve this, you need change your else statements into additionalif statements (so instead of being one or the other, you can potentially get both actions - jump & right).

Here is a code example:

function gameLoop(loopEvent: Event): void {
    //If the right key is pressed, and the left key is NOT pressed.
    if (rightPressed && !leftPressed) {
        naruto.x += narutoSpeed;
        naruto.gotoAndStop("right");
    }

    if(leftPressed && !rightPressed) {
        naruto.x -= narutoSpeed;
        naruto.gotoAndStop("left");
    }

    if (upPressed) {
        //if up is pressed, and right is pressed
        if(rightPressed && !leftPressed) naruto.gotoAndStop("jumpright");
        if(leftPressed && !rightPressed) naruto.gotoAndStop("jumpleft");
    }

    if(leftPressed && rightPressed && !upPressed){
        naruto.gotoAndStop("stance");  //if left & right are pressed, they negate each other and you play stance
    }
}

With this code, if the right key is pressed, naruto will move right. If up is also pressed, then in addition to moving right, it will switch from the right state to the jumpright state.

Upvotes: 2

Related Questions