I'm creating a beat em up game and so far i have the character running and jumping. I now want to make my character to dash left or right. So if the player presses the key right, right very quickly then the character will dash. How can i make this happen. This is what I have done so far.
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 Jumped: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;
}
//If on floor
if (CanJump)
{
//If right key is pressed run right
if ((RightKeyPress))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress))
{
//otherwise if left key is pressed run left
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
if ((UpKeyPress))
{
//If up key is pressed then jump
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
Jumped = true;
}
//If no key is pressed stay idle
if ((!RightKeyPress && !LeftKeyPress && CanJump))
{
gotoAndStop('Idle');
}
}
else if (CanJump == false)
{
//Other if in air and right key is pressed move right
if ((RightKeyPress))
{
x += RunSpeed;
scaleX = 1;
}
else if ((LeftKeyPress))
{
//Otherwise if left key is pressed then move left
x -= RunSpeed;
scaleX = -1;
}
}
//If already jumped and on floor
if (Jumped == true && CanJump)
{
//Cannot jump again
CanJump = false;
//If on floor and right key is pressed run right
if ((RightKeyPress))
{
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress))
{
//Otherwise if on floor and left key is pressed run left
gotoAndStop('Run');
scaleX = -1;
}
//If no key is pressed stay idle
if ((!RightKeyPress && !LeftKeyPress))
{
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;
Jumped = false;
}
}
}
}
So I made a little test and came up with this. It seems to be working pretty well right now. I haven't tested it in extreme lengths, but it could get you started. I'll leave it up to you to implement into your own code. Just copy/paste into a new AS3 document and watch the console
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressed);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function keyPressed(e:KeyboardEvent):void
{
if (lastKeyPressed == e.keyCode && pressed) trace("double tapped " + e.keyCode);
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false},doubleTapDelay);
}
EDITED TO WORK WITH KEY_DOWN
This seems to be working better with KEY_DOWN
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function keyPressed(e:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
if (lastKeyPressed == e.keyCode && pressed) trace("double tapped " + e.keyCode);
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false},doubleTapDelay);
}
function onKeyboardUp(e:KeyboardEvent):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
EDITED AGAIN FOR THE DEMO
DEMO: http://ronnieswietek.com/_random/dash_example.swf
SOURCE: http://ronnieswietek.com/_random/dash_example.fla
import flash.events.KeyboardEvent;
import com.greensock.TweenLite;
import com.greensock.easing.*;
import com.greensock.plugins.*;
TweenPlugin.activate([BlurFilterPlugin]);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_DOWN, permaKeyDown);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var dashAmount :Number = 50;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function permaKeyDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 38: //-- up arrow
char.y = char.y - 2;
break;
case 39: //-- right arrow
char.x = char.x + 2;
break;
case 40: //-- down arrow
char.y = char.y + 2;
break;
case 37: //-- left arrow
char.x = char.x - 2;
break;
}
}
function keyPressed(e:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
if (lastKeyPressed == e.keyCode && pressed)
{
trace("double tapped " + e.keyCode);
doDash(e.keyCode);
}
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false}, doubleTapDelay);
}
function onKeyboardUp(e:KeyboardEvent):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
function doDash(keyCode:Number):void
{
switch (keyCode)
{
case 38: //-- up arrow
TweenLite.to(char,0,{blurFilter:{blurY:50}});
TweenLite.to(char,0.3,{blurFilter:{blurY:0},y:char.y - dashAmount,ease:Expo.easeOut});
break;
case 39: //-- right arrow
TweenLite.to(char,0,{blurFilter:{blurX:50}});
TweenLite.to(char,0.3,{blurFilter:{blurX:0},x:char.x + dashAmount,ease:Expo.easeOut});
break;
case 40: //-- down arrow
TweenLite.to(char,0,{blurFilter:{blurY:50}});
TweenLite.to(char,0.3,{blurFilter:{blurY:0},y:char.y + dashAmount,ease:Expo.easeOut});
break;
case 37: //-- left arrow
TweenLite.to(char,0,{blurFilter:{blurX:50}});
TweenLite.to(char,0.3,{blurFilter:{blurX:0},x:char.x - dashAmount,ease:Expo.easeOut});
break;
}
}
Related
So this is my first semester in school coding and right now I'm trying to make a little 2D game with a character who I need to move up and down.
So far, I've been able to create a titlescreen and then when you click start it goes to the next screen, where my main character is.
I add him to the stage manually through the code, and then I tried to make him move up down left and right with the arrow keys but he only appears, and does not move.
This is my code so far
package lib.fly
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class FlyGame extends MovieClip
{
public var mainCharacter:MovieClip;
public var vx:Number;
public var vy:Number;
public function FlyGame()
{
trace ("Initiate");
init();
}
private function init():void
{
vx = 0;
vy = 0;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveAround);
stage.addEventListener(KeyboardEvent.KEY_UP, dontMove);
//var dx:Number = speed* Math.cos(angle);
//var dy:Number = speed* Math.sin(angle);
trace ("Keyboard Event Listeners");
}
private function moveAround(event:KeyboardEvent):void
{
trace ("Actual Keyboard Events");
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
}
else if (event.keyCode == Keyboard.UP)
{
vy = - 5;
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
}
}
private function dontMove(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
{
vx = 0;
}
else if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN)
{
vy = 0;
}
}
public function onEnterFrame(event:Event):void
{
mainCharacter = new BoyFlying();
mainCharacter.x = 20;
mainCharacter.y = 150;
mainCharacter.x += vx;
mainCharacter.y += vy;
addChild(mainCharacter);
}
}
}
The output produces the trace statements up until my "Actual Keyboard Events"
Sorry, I'm brand new to this so any help would be appreciated. Thank you for your time
Try this logic below and see if it helps your program to work as expected...
Adjust function init()
private function init():void
{
vx = vy = 0; //chain them since same value
mainCharacter = new BoyFlying(); //create once here and control in other functions
mainCharacter.x = 20;
mainCharacter.y = 150;
mainCharacter.addEventListener(Event.ENTER_FRAME, onEnterFrame);
addChild(mainCharacter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveAround);
//stage.addEventListener(KeyboardEvent.KEY_UP, dontMove); //what is it good for?
trace ("added... Keyboard Event Listeners");
//var dx:Number = speed* Math.cos(angle);
//var dy:Number = speed* Math.sin(angle);
}
Adjust function moveAround()
private function moveAround(event:KeyboardEvent):void
{
trace ("Actual Keyboard Events");
if (event.keyCode == Keyboard.LEFT)
{ vx -= 5; }
if (event.keyCode == Keyboard.RIGHT)
{ vx += 5; }
if (event.keyCode == Keyboard.UP)
{ vy -= 5; }
if (event.keyCode == Keyboard.DOWN)
{ vy += 5; }
}
Adjust function onEnterFrame()
public function onEnterFrame(event:Event):void
{
//# no need for += here since function moveAround() changes these vx and vy values on key press
//# infact your character could be moved (x/y) just by keyboard event instead of per FPS screen update (eg: not with EnterFrame)
mainCharacter.x = vx;
mainCharacter.y = vy;
}
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
}
}
I'm making a platform game. Im trying just to get the player to move around on the stage, and to be able to jump, with some type of gravity added to it. However, when I run it, I get this error: Error #1009: Cannot access a property or method of a null object reference.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code1 extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
private var platform:Platform;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function Code() {
}
public function update(evt:Event){
moveChar();
}
public function moveChar(){
if (leftKey == true){
charSpeed -= 10;
}
if (rightKey == true){
charSpeed += 10;
}
if (upKey == true){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
}
function loop(evt:Event){
player.x = charSpeed;
if (player.x < 0){
player.x = 0;
}
if (player.x > 550){
player.x = 550;
}
velocity += gravity;
if (!platform.hitTestPoint(player.x, player.y, true)){
player.y += velocity;
}
for (var i = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
player.y--;
velocity = 0;
Jump = false;
}
}
}
}
}
My platform linkage is "Platform", but i set up a variable for it (or tried to). I debugged the code, and it came up with this line: player.x = charSpeed;
I have no idea what to do, if someone could help, that would be great.
You never declare or instantiate (ie player = new Player()) your player.
Alternatively, if your player is on the stage of your .fla timeline, it will need the instance name 'player'. That can be set in the object properties.
You player object is null.
I don't see the istantiation line as:
var player:Player = new Player();
Add it before use a property of your player
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.
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