Let's learn how to can render a sprite:
- Find an image of a spaceship and upload it to the assets folder. In my example, I'm using the phaser-ship.png file.
- Now, load the image into the game as a ship asset:
function preload() {
this.load.setBaseURL('.');
this.load.image('background', 'assets/background.jpg');
this.load.image('ship', 'assets/phaser-ship.png');
}
- Next, create a global variable called ship. This is going to contain our spaceship sprite:
const game = new Phaser.Game(config);
let ship;
- Now, assign the ship variable from within the create function. We need to reuse the variable inside the update function:
// Create ship
ship = this.add.sprite(100, 100, 'ship');
Notice how we passed the initial coordinates and the asset...