I just implemented that in my own platformer. I used tweens to accomplish this. Here’s my implementation:
(For some reason the code is all wrangled up. I hope it’s still readable.)
// In Player class:
private var isJumping:Boolean = false;
private var jumpAlarm:Alarm = null;
[...]
// in update()
if (Input.pressed("jump"))
{
jump();
}
else if (Input.released("jump"))
{
stopJump();
}
[...]
if (isJumping)
{
velY = -GC.PlayerJumpStrength;
}
[...]
if (!onGround)
{
velY += Physics.gravity;
velY = FP.clamp(velY, -16, 16);
}
// jump()
public function jump():void
{
// If pressed "jump" and down and standing on a platform, fall through.
if (onPlatform && Input.check("down"))
{
onGround = false;
pPlatform.removeEntity(this);
onPlatform = false;
pPlatform = null;
moveBy(0, 2, "solid", true);
} else if (onGround)
{
onGround = false;
isJumping = true;
jumpAlarm = FP.alarm(GC.PlayerJumpDuration, stopJump);
if (onPlatform)
{
pPlatform.removeEntity(this);
onPlatform = false;
pPlatform = null;
}
}
}
// stopJump()
public function stopJump():void
{
if (jumpAlarm)
{
jumpAlarm.cancel();
jumpAlarm = null;
}
isJumping = false;
}