jaybird
September 10, 2020, 6:18pm
1
I want to temporarily stop the player from moving for a couple of seconds after it collides with an item in the game. I’ve added a function in a
player class for that purpose and managed to disable the controls, but the player keeps moving after the collision.
Here is the function I wrote:
specialItemCollected()
{
this.scene.input.keyboard.enabled = false
this.scene.time.delayedCall(2000, () => {
this.scene.input.keyboard.enabled = true
}, [], this);
}
The pictures illustrate the effect I want to achieve:
BlunT76
September 10, 2020, 7:57pm
2
Hi
Try this syntax:
this.scene.time.addEvent({
delay: 2000,
callback: () => {
this.scene.input.keyboard.enabled = true
}
});
jaybird
September 10, 2020, 8:22pm
3
No that didn’t work. The player keeps moving even though I release the arrow key after the collision
However, I solved it by adding a boolean variable called isPlayerMovable which by default is true:
foundItem()
{
this.isPlayerMovable = false
this.scene.time.delayedCall(2000, () => {
this.isPlayerMovable = true
}, [], this);
}
...
update(cursors: Phaser.Types.Input.Keyboard.CursorKeys)
{
...
if (this.isLeftKeyDown && this.isPlayerMovable)
{...}
}