How to get a sprite to scale in Phaser

Viewed 46

I'm importing an image from a JSON spritesheet, which works okay. However, I want to get the sprite to scale once it is in the game. Using the code below, I get the error in console:

Uncaught TypeError: plane.scale is not a function

    const config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    scene: {
        preload: preload,
        create: create
        }
    };

    const game = new Phaser.Game(config);

    function preload () {
        this.load.image('background', 'assets/1.jpg');
        this.load.multiatlas('aircraft', 'assets/texture.json', 'assets');
    }

    function create () {
        const background = this.add.image(800, 600, 'background');
        const plane = this.add.sprite(200, 200, 'aircraft', 'Glider2.png');
        plane.scale(0.1, 0.1);
    }
1 Answers

The error is caused, because the function scale does not exist (scale is a property), you would need to use the function setScale (link to the documentation).

The code should look like this:

...
const plane = this.add.sprite(200, 200, 'aircraft', 'Glider2.png');
plane.setScale(0.1);
...

btw: if the scale-value should be the same value on the x and the y axis you can omit the second parameter.

Related