Character jumping while moving right and left AS3 - actionscript-3

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!

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.

Related

Character jumping but not returning to ground platform game AS3

I am making a platform game where the main character moves right and left and jumps however my character jumps and does not return to the ground but stays on top of the stage.My characters movie-clip symbol is called 'naruto' and my ground symbol is called 'ground'.
Here is my code:
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 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");
}
var jumpHeight =0;
var defaultJumpSpeed = 20;
var jumpSpeed = 20;
if(upPressed && naruto.hitTestObject(ground))
{
trace("HELLO!");
naruto.y -= jumpSpeed;
jumpSpeed-= 4;
}
if(upPressed)
{
trace("HELLO!");
jumpHeight++;
naruto.y -= jumpSpeed;
if(jumpHeight>10)
jumpSpeed -= 4;
}
if(naruto.hitTestObject(ground))
{
trace("HELLO!");
jumpHeight =0;
jumpSpeed = defaultJumpSpeed;
}
}
Here is the link for my work: https://www.mediafire.com/?8d5opy49fuqmup5
Here is the problem:
The main issue, is in your current code, the player can only possible be in 3 positions:
Whatever the ground position is
16 (up is pressed and the character was not touching the ground
12 (up is pressed and the character was touching the ground)
Please see the code comments next to your original code to explain what is happening:
//you have this var inside the game loop, so every loop it resets to 20
var jumpSpeed = 20;
//so here, if up is pressed and the character is touching the ground
//you initiate a jump
if(upPressed && naruto.hitTestObject(ground))
{
trace("HELLO!");
naruto.y -= jumpSpeed;
jumpSpeed-= 4; //since you reset jumpSpeed to 20 every loop, this value will always just be 16
}
if(upPressed)
{
trace("HELLO!");
jumpHeight++;
//if naruto is touching the ground
//you are subtracting jumpSpeed above and right here again.
//so now the y position is 12 if touching the ground (or 16 if not touching the ground)
//since you reset jumpSpeed to 20 every loop, it always be one of those two values
naruto.y -= jumpSpeed;
if(jumpHeight>10)
jumpSpeed -= 4; //this serves no purpose, as your not using the value again and it gets reset to 20 next time the game loop runs
}
//this hit test will succeed in addition to the first one above when your jump starts.
if(naruto.hitTestObject(ground))
{
trace("HELLO!");
jumpHeight =0;
jumpSpeed = defaultJumpSpeed;
}
To remedy your jumping, you'll need to do something along these lines:
//initialize these vars outside of the game-loop for efficient and proper scoping (so their values don't reset every frame)
var isJumping:Boolean = false; //a flag to know if a jump is in progress
var jumpSpeed:Number = 0; //the current velocity of the jump
var defaultJumpSpeed:Number = 20; //the initial force (speed) of a jump
var jumpGravity:Number = 2; //subtract this from the speed every frame to slow the jump over time and fall
var onGround:Boolean = false; //a helper var for efficiency
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");
}
//only do the hit test once per frame for efficiency (store the result)
onGround = naruto.hitTestObject(ground);
if (upPressed && onGround) {
trace("START JUMP");
isJumping = true;
jumpSpeed = defaultJumpSpeed; //set the initial jump velocity
}
if(isJumping){ //if jumping
naruto.y -= jumpSpeed; //move the player based off the current jump velocity
jumpSpeed -= jumpGravity; //slow the jump every frame (eventually causing it to be negative making the player fall)
}
//touching the ground and the up key is not pressed
//it's very important to put !upPressed so this doesn't run at the time as the initial jump above
if (!upPressed && onGround) {
//stop any jump motion
jumpSpeed = 0;
isJumping = false;
//you probably also want to snap the player to the ground
naruto.y = ground.y - naruto.height + 2; //the plus 2 ensures that the ground and the player touch so the hit test succeeds
}
}

Linking multiple objects to one coding name

I am trying to make an rpg style game in Flash AS3. I am trying to get my character to stop when he hits objects such as trees and buildings. Is there a way I can link multiple objects together and make the code say something like 'take object1, object2, and object3 and name it multipleobjects'? I have the code set up so that the character stops at one tree but I'm not positive how/if you can combine objects so that he won't go through multiple at one time. Thank you very very much in advanced!
Objects:
manmc (my character), treer1_MC (the first tree) treer2_MC (second tree) and so on
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
manmc.gotoAndStop ("Stand Front Frame");
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 manSpeed:Number = 3;
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;
manmc.gotoAndStop("Stand Right Frame");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
manmc.gotoAndStop("Stand Left Frame");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
manmc.gotoAndStop("Stand Back Frame");
}
else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
manmc.gotoAndStop("Stand Front Frame");
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed)
{
if(manmc.x < 1050)
{
manmc.x += manSpeed;
}
manmc.gotoAndStop("Walk Right Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("leftHit");
manmc.x -= 3;
}
}
else if(leftPressed)
{
if (manmc.x > 145)
{
manmc.x -= manSpeed;
}
manmc.gotoAndStop("Walk Left Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("rightHit");
manmc.x += 3;
}
}
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("downHit");
manmc.y -= 3;
}
}
else if(upPressed)
{
if(manmc.y > 145)
{
manmc.y -= manSpeed;
}
manmc.gotoAndStop("Walk Back Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("upHit");
manmc.y += 3;
}
}
}
Use an array to keep track of multiple objects.
Instead of manipulating the single object, you then use a loop to iterate over all the elements in the array which are the single objects.

Object Barriers in AS3?

I am a beginner at coding but I am trying to make a flash rpg type game. I have a barrier around my game and a walking person (manmc). I have been watching videos/reading articles on how to make object barriers but I can not seem to get the coding right without getting errors. I am trying to get my character to stop when he hits a tree. The tree is coded as (treer1_MC). I only tried to code it on the down press but it does nothing and it causes the character to not be able to walk back up.
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
manmc.gotoAndStop ("Stand Front Frame");
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 manSpeed:Number = 3;
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;
manmc.gotoAndStop("Stand Right Frame");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
manmc.gotoAndStop("Stand Left Frame");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
manmc.gotoAndStop("Stand Back Frame");
}
else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
manmc.gotoAndStop("Stand Front Frame");
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed)
{
if(manmc.x < 1050)
{
manmc.x += manSpeed;
}
manmc.gotoAndStop("Walk Right Frame");
}
else if(leftPressed)
{
if (manmc.x > 145)
{
manmc.x -= manSpeed;
}
manmc.gotoAndStop("Walk Left Frame");
}
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
}
if (manmc.hitTest(treer1_MC))
{
trace("leftHit");
manmc._x += 3;
}
else if(upPressed)
{
if(manmc.y > 145)
{
manmc.y -= manSpeed;
}
manmc.gotoAndStop("Walk Back Frame");
}
}
This is the coding a tutorial said to use. Any suggestions?
if (manmc.hitTest(treer1_MC))
trace("leftHit");
manmc._x += 3;
It is just because the declaration of the collision condition is outside of your "downPress" condition:
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
}
if (manmc.hitTest(treer1_MC))
{
trace("leftHit");
manmc._x += 3;
}
indentation problems :), the right way should be:
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
if (manmc.hitTest(treer1_MC))
{
trace("leftHit");
manmc._x += 3;
}
}
I hope it helps ;)
Edit:
manmc.hitTest(treer1_MC)
should be:
manmc.hitTestObject(treer1_MC)
Because the function: hitTest() is a AS2 function, on AS3 whe have: hitTestPoint() and hitTestObject() ;)

AS3: Single Key Press Only

I'm trying to create a platform game and I have the run, jump and double jump working. Now my problem is I want the player to press the jump button and the hero to jump only once instead of continuously jumping. If I hold the up button it will keep jumping which i do not want, i just want it to perform the jump once even if the key is still held down. I want the same for the double jump, how can i do this.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
//Player run speed setting
var RunSpeed:Number = 8;
//Player key presses
var RightKeyPress:Boolean = false;
var LeftKeyPress:Boolean = false;
var UpKeyPress:Boolean = false;
//Jump variables
var Gravity:Number = 1.5;
var JumpPower:Number = 0;
var CanJump:Boolean = false;
var DoubleJump:Boolean = false;
public function Player()
{
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
addEventListener(Event.ENTER_FRAME,Update);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyReleased);
}
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true
}
}
function Update(event:Event)
{
//Adding gravity to the game world
JumpPower += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
JumpPower = 0;
CanJump = true;
DoubleJump = false;
}
//If player is on floor and right key is pressed then run right
if ((RightKeyPress && CanJump))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress && CanJump))
{
//Otherwise if on floor and left key is pressed run left
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
else if ((UpKeyPress && CanJump))
{
//Otherwise if on floor and up key is pressed then jump
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If on floor and right and up key are pressed then jump
if ((RightKeyPress && UpKeyPress && CanJump))
{
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If on floor and left and up key are pressed then jump
if ((LeftKeyPress && UpKeyPress && CanJump))
{
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If jumped and right key is pressed then move right
if ((RightKeyPress && CanJump == false))
{
x += RunSpeed;
scaleX = 1;
}
else if ((LeftKeyPress && CanJump == false))
{
//Otherwise if jumped and left key is pressed then move left
x -= RunSpeed;
scaleX = -1;
}
//If in air and able to doublejump and pressed up key, then double jump
if (UpKeyPress && DoubleJump && JumpPower > -2)
{
JumpPower = -13;
DoubleJump = false;
gotoAndStop('DoubleJump');
}
//If on floor and no key is presses stay idle
if ((!RightKeyPress && !LeftKeyPress && CanJump))
{
gotoAndStop('Idle');
}
this.y += JumpPower;
}
function KeyReleased(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
event.keyCode = 0;
RightKeyPress = false;
}
if (event.keyCode == 37)
{
event.keyCode = 0;
LeftKeyPress = false;
}
if (event.keyCode == 38)
{
event.keyCode = 0;
UpKeyPress = false;
}
}
}
}
I suspect that what is happening is that as soon as your player's y value meets the condition if (this.y > 300) you are enabling him to jump again, so as long as the key is held down he jumps because UpKeyPress && CanJump are both true.
My guess is that doing something like the following might get you closer yo your answer...
In your Update function:
function Update(event:Event)
{
//Adding gravity to the game world
JumpPower += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
JumpPower = 0;
// Do not allow another jump until the UpKey is pressed
//CanJump = true;
//DoubleJump = false;
}
...
Then in your KeyPressed function:
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true
// Do not allow another jump until the UpKey is pressed
if (this.y > 300)
{
CanJump = true;
DoubleJump = false;
}
}
}
I also second shaunhusain's recommendation to set breakpoints and get comfortable with debugging code.

Character Jump with Animation

Im trying to create a platform game but im struggling with the jump part of the game. I have got the jump working but when I try to add the animation it goes all wrong. When I hit the jump button it jumps but when i lands it gets stuck on the end of the jump animation until i move. Secondly when i jump and press the right or left button it jumps but plays the run animation while i try to move in mid air. How can i resolve these problems
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
//Player run speed setting
var RunSpeed:Number = 8;
//Player key presses
var RightKeyPress:Boolean = false;
var LeftKeyPress:Boolean = false;
var UpKeyPress:Boolean = false;
//Jump variables
var Gravity:Number = 1.5;
var Yvelocity:Number = 0;
var CanJump:Boolean = false;
public function Player()
{
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
addEventListener(Event.ENTER_FRAME, Update);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyReleased);
}
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true;
}
}
function Update(event:Event)
{
//Adding gravity to the game world
Yvelocity += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
Yvelocity = 0;
CanJump = true;
}
if ((RightKeyPress == true))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress == true))
{
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
if ((UpKeyPress == true && CanJump))
{
Yvelocity = -15;
CanJump = false;
gotoAndStop('Jump');
}
this.y += Yvelocity;
}
function KeyReleased(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
event.keyCode = 0;
RightKeyPress = false;
gotoAndStop('Idle');
}
if (event.keyCode == 37)
{
event.keyCode = 0;
LeftKeyPress = false;
gotoAndStop('Idle');
}
if (event.keyCode == 38)
{
event.keyCode = 0;
UpKeyPress = false;
}
}
}
}
this.y > 300, that is your jump end logic. Add a gotoAndStop('Idle'); in that if's block.
Just Check if canJump is true on your left and right keypresses before executing those blocks. If canJump is true allow the running stuff