Just like the background, the original image for our spaceship may be either too big or too small. In my case, it is tiny, and I need to scale it a bit. Let's see how we can do that:
- To set custom scaling in the form of a float number, use the sprite.setScale(x, y) method:
// Create ship
ship = this.add.sprite(100, 100, 'ship');
ship.setScale(4, 4);
- Along with the global variables, the create implementation of our game should look as follows:
const game = new Phaser.Game(config);
let ship;
function create() {
// Create background
const image = this.add.image(
this.cameras.main.width / 2,
this.cameras.main.height / 2,
'background'
);
let scaleX = this.cameras.main.width / image.width;
let scaleY = this.cameras.main.height / image.height;
...