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).
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!