I have created a jumping functionality for a player in the update()
function like so:
if (Phaser.Input.Keyboard.JustDown(this.arrowKeys.up)) {
//Set ollie power
ollie = -12;
//Set skate velocity
skater.setVelocityY(ollie);
//Play the ollie animation
skater.anims.play('ollie');
}
It works well, but the player is able to keep jumping mid-air. I know if I would be using arcade
physics I could prevent that like so:
if (cursors.up.isDown && player.body.touching.down)
{
player.setVelocityY(-330);
}
But with the matter
physics won’t work…
I have found a way to check if the player is hitting the floor in the create()
function like so:
checkPlayerCollisionWithFloor = () => {
this.matter.world.on("collisionactive", (skater, ground) => {
console.log('player hit the floor');
return true;
});
}
Now I tried to use this in my update()
function like so:
if ( Phaser.Input.Keyboard.JustDown(this.arrowKeys.up) && checkPlayerCollisionWithFloor() ) {
//Set ollie power
ollie = -12;
//Set skate velocity
skater.setVelocityY(ollie);
//Play the ollie animation
skater.anims.play('ollie');
}
I tried different way to pass the function in if else
satement, but it doesn’t work. I need help please.