In this section, we are going to provide keyboard support for our minigame. Our users should be able to use the cursor keys to move the ship in all directions. To access the state of the keyboard keys, we need a global variable to hold the state of the pressed keys. Let's learn how to do this:
- Let's call it cursors and put it under the game instance:
const game = new Phaser.Game(config);
let cursors;
- The create function allows you to access the input.keyboard object. You can use this object to retrieve a reference to the cursor keys' state:
// Create cursors
cursors = this.input.keyboard.createCursorKeys();
According to the official documentation, this creates a new object called cursors. It contains four objects: up, down, left, and right. These are all Phaser.Key objects, so anything you can do with a Key object...