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 if (sprite.isTouching(FlxObject.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;