Key Down Delay

Demonstrates how to use the checkDown method to apply a custom rate-limit or delay to keyboard input, controlling how often an action triggers while a key is held down.

Key Down Delay, running
1024 × 768
GitHub
class Example extends Phaser.Scene
{
    player;
    cursors;

    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.image('block', 'assets/sprites/block.png');
    }

    create ()
    {
        this.cursors = this.input.keyboard.createCursorKeys();

        this.player = this.add.image(400, 300, 'block');
    }

    update ()
    {
        //  Horizontal movement every 250ms
        if (this.input.keyboard.checkDown(this.cursors.left, 250))
        {
            this.player.x -= 32;
        }
        else if (this.input.keyboard.checkDown(this.cursors.right, 250))
        {
            this.player.x += 32;
        }

        //  Vertical movement every 150ms
        if (this.input.keyboard.checkDown(this.cursors.up, 150))
        {
            this.player.y -= 32;
        }
        else if (this.input.keyboard.checkDown(this.cursors.down, 150))
        {
            this.player.y += 32;
        }
    }
}

const config = {
    scale: { mode: Phaser.Scale.FIT },
    type: Phaser.AUTO,
    parent: 'game-container',
    backgroundColor: '#0072bc',
    scene: Example
};

const game = new Phaser.Game(config);

More in Keyboard22 examples