There are many ways to implement a jumping mechanic, but the simplest method is to add negative y velocity
temporarily to a FlxObject
with positive y acceleration
, using some kind of timer to limit the jump time.
FlxG.collide(box, sprite);
var jumpPressed:Bool = pad.buttonA.pressed;
if (jumping && !jumpPressed)
jumping = false;
/*
* Reset jumpTimer when touching the floor
* Note: This sprite's touching flags are set via FlxG.collide,
* and are reset to false on super.update
*/
if (sprite.isTouching(DOWN) && !jumping)
jumpTimer = 0;
if (jumpTimer >= 0 && jumpPressed)
{
jumping = true;
jumpTimer += elapsed;
}
else
jumpTimer = -1;
// hold button to jump higher (up to 0.25s)
if (jumpTimer > 0 && jumpTimer < 0.25)
sprite.velocity.y = -300;