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.
isDownHowever, 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.


コメント