I have finally found the solution for this issue:
-
First, declare a variable called
skaterTouchingGround
right after theconfig
object like so:
let skaterTouchingGround;
-
Second, in the
create()
function add the collision detector event & set the variableskaterTouchingGround
totrue
like so:
//Detect collision with ground
this.matter.world.on("collisionactive", (skater, ground) => {
skaterTouchingGround = true;
});
-
Third, in the
update()
function &if
statement, add theskaterTouchingGround
variable as condition to trigger the jump & once the jump is done set the variableskaterTouchingGround
tofalse
like so:
//Ollie
if (Phaser.Input.Keyboard.JustDown(this.arrowKeys.up) && skaterTouchingGround) {
skaterTouchingGround = false;
//Set ollie power
ollie = -12;
//Set skate velocity
skater.setVelocityY(ollie);
//Play the ollie animation
skater.anims.play('ollie');
}
If you follow these steps, the skater won’t be able to jump midair.