This is one of the most common questions out there but one of the most difficult to understand when it comes to noobs like me. I've been working on this for days and still I haven't made any progress. So here is the problem. I have my character moving and have all these animations. He runs facing right. He runs facing left. He jumps up and comes back down but here is what I want him to do: Jump once every key pressed down. He's like a kangaroo whenever the key is held down. So here it is (out of frustration), how do you code a jump once code? Here's my code if anyone out there would like to take a look:
jeffHero_mc.gotoAndStop('idle');
var Key:KeyObject = new KeyObject(stage);
var dy:Number = 0;
var gravity:Number = 1;
var canjump:Boolean = false;
stage.addEventListener(Event.ENTER_FRAME, onenter);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
function keyPressed(event:KeyboardEvent):void
{
if(Key.isDown(Key.SPACE) && canjump == true)
{
canjump = false;
jump();
}
}
}
function keyReleased(event:KeyboardEvent):void
{
if(Key.isDown(Key.SPACE))
{
canjump = false;
}
}
function onenter(e:Event):void
{
dy+=gravity;
if(jeffHero_mc.y>=300)
{
dy=0;
canjump=true;
}
jeffHero_mc.y+=dy;
for(var i:int = 0; i<.03; i++)
{
if(floor_mc.hitTestPoint(jeffHero_mc.x, jeffHero_mc.y, true))
{
dy=0;
canjump=false;
}
}
if(Key.isDown(Key.RIGHT))
{
jeffHero_mc.x+=5;
jeffHero_mc.scaleX=0.3;
jeffHero_mc.gotoAndStop('walking');
}else if(Key.isDown(Key.LEFT))
{
jeffHero_mc.x-=5;
jeffHero_mc.scaleX=-0.3;
jeffHero_mc.gotoAndStop('walking');
}else if(Key.isDown(Key.SPACE))
{
jeffHero_mc.y-=15;
jeffHero_mc.scaleX-=0.3;
jeffHero_mc.scaleX+=0.3;
jeffHero_mc.gotoAndStop('jumping');
}
{
if(!Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT) && !Key.isDown(Key.SPACE))
{
jeffHero_mc.gotoAndStop('idle');
}
}
}