1
votes

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!

1
Please edit your question to include the code you're trying to use. While a link to your project can be helpful, not everyone is comfortable download files from strangers and everything needed to solve your question should be included in the question itself.BadFeelingAboutThis
Ok, sir, i will add my code.Hamza
Do you have any idea of how to solve this?Hamza
I'm still having a hard time understanding what the problem is. You want two keys to have to be pressed for left/right walking and you don't know how to handle simultaneous keystrokes? Or are you getting some kind of error or unexpected result?BadFeelingAboutThis
No, i want the character to face right when jumping if necessary and face left when jumping when necessary.Hamza

1 Answers

2
votes

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.