View Full Version : jumping character - disable when falling
ryrocks
November 17th, 2008, 03:12 PM
..
GrndMasterFlash
November 17th, 2008, 03:43 PM
have a second var for the falling, and if jumping != true and your not touching the ground, your falling, if your falling you can't jump, that's usually what i do, in the begining (or end) have falling = true then have your check to set it to false if you on ground, dig? :cap:
ryrocks
November 17th, 2008, 05:10 PM
YEAH BOY! I nearly sussed that before, thanks for the direction.
Here's the new improved code if anyone's interested:
var leftDown:Boolean = false;
var rightDown:Boolean = false;
var jump:Boolean = false;
var velocity:Number = 15;
var falling:Boolean = true;
stage.addEventListener(KeyboardEvent.KEY_DOWN, downDetect);
function downDetect(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.LEFT)
{
leftDown = true;
}
if(event.keyCode == Keyboard.RIGHT)
{
rightDown = true;
}
if(event.keyCode == Keyboard.UP && !falling)
{
jump = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, upDetect);
function upDetect(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.LEFT)
{
leftDown = false;
}
if(event.keyCode == Keyboard.RIGHT)
{
rightDown = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void
{
if (leftDown)
{
ball_mc.x -= 5;
}
if (rightDown)
{
ball_mc.x += 5;
}
if (jump && !falling)
{
ball_mc.y -= velocity;
velocity -= 1.5;
}
}
stage.addEventListener(Event.ENTER_FRAME, hitCheck);
function hitCheck(event:Event):void
{
if (ball_mc.hitTestObject(ground_mc))
{
jump = false;
falling = false;
velocity = 15;
}
else if (!ball_mc.hitTestObject(ground_mc) && !jump)
{
falling = true;
jump = false;
ball_mc.y += 16;
}
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.