Execute any function by typing on the keyboard (Phaser3)

この記事は約3分で読めます。
スポンサーリンク

Things I want to do

When a key is pressed in Phaser3, an arbitrary function is executed.

Register using the following method:

this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);

Within the update statement, you can execute arbitrary code by obtaining the following values ​​from the return value mentioned above.

isDown

However, I think it’s best not to write key events other than actions (such as pause or retry) inside the Update block.

スポンサーリンク

Implementation using ‘on’

This can be implemented using On as follows:

In most cases, it’s best to call it using Create.

The listener is executed when the Space key is pressed.

        this.input.keyboard.on('keydown-SPACE', listener);

The ‘SPACE’ part of ‘keydown-SPACE’ can be changed to any key. Changing it to ‘keydown-A’ will cause it to execute when the A key is pressed.

A listener is a function that takes an Event as an argument.

You can also write it like this without defining a function:

        this.input.keyboard.on('keydown-SPACE', event =>
        {
            //処理
        });
スポンサーリンク

Result

It is now possible to process input without using Update when a key is pressed.

スポンサーリンク

Websites I used as references

Phaser 3 Examples

コメント

タイトルとURLをコピーしました