setVelocityX error

Hi,

I have this error message when i put setVelocityX in my update section (in the condition if score > 50 ):
this.body is undefined

i don’t understand why i have this message. Someone have an idea please ?

Blockquote class Scene2 extends Phaser.Scene {

constructor() {

    super("playgame");

}

/* --------------- CREATE SECTION -------------------- */

create() {

    this.background = this.add.tileSprite(0, 0, config.width, config.height, "background");

    this.planet = this.add.tileSprite(0, 0, config.width, config.height, "planet");

    this.stars = this.add.tileSprite(0, 0, config.width, config.height, "stars");

    this.background.setOrigin(0, 0);

    this.planet.setOrigin(0, 0);

    this.stars.setOrigin(0, 0);

    /*-------------------------------------------*/

    /*--------------------------------------------*/

    var graphics = this.add.graphics();

    graphics.fillStyle(0x000000, 1);

    graphics.beginPath();

    graphics.moveTo(0, 0);

    graphics.lineTo(config.width, 0);

    graphics.lineTo(config.width, 25);

    graphics.lineTo(0, 25);

    graphics.lineTo(0, 0);

    //

    graphics.closePath();

    graphics.fillPath();

    this.score = 0;

    var scoreFormated = this.zeroPad(this.score, 6);

    this.scoreLabel = this.add.bitmapText(10, 5, "pixelFont", "SCORE " + scoreFormated, 30);

    /*   -------------------------- sound --------------------------*/

    this.beamSound = this.sound.add("audio_beam");

    this.explosionSound = this.sound.add("audio_explosion");

    this.pickupSound = this.sound.add("audio_pickup");

    this.music = this.sound.add("music");

    var musicConfig = {

        mute: false,

        volume: 1,

        rate: 1,

        detune: 0,

        seek: 0,

        loop: true,

        delay: 0

    }

    this.music.play(musicConfig);

    /* --------------------------enemy----------------------------- */

    this.ship1 = this.add.sprite(config.width / 2 - 100, config.height / 1, "ship");

    this.ship2 = this.add.sprite(config.width / 2, config.height / 1, "ship2");

    this.ship3 = this.add.sprite(config.width / 2 + 100, config.height / 1, "ship3");

    this.ufo = this.physics.add.image(0, config.height / 2, "enemy");

    this.ufo.setActive(false);

    this.ufo.setVisible(false);

    this.ufo.setCollideWorldBounds(true);

    this.ufo.setBounce(1);

 

    this.ennemies = this.physics.add.group();

    this.ennemies.add(this.ship1);

    this.ennemies.add(this.ship2);

    this.ennemies.add(this.ship3);


    this.time.addEvent({

        loop: true,

        callback: this.addShip

    });

    this.ship1.play("ship1_anim");

    this.ship2.play("ship2_anim");

    this.ship3.play("ship3_anim");

    this.ship1.setInteractive();

    this.ship2.setInteractive();

    this.ship3.setInteractive();

    this.input.on('gameobjectdown', this.destroyShip, this);

    this.ship1.setScale(0.25);

    this.ship2.setScale(0.25);

    this.ship3.setScale(0.25);

    this.ufo.setScale(0.10);

    this.ship1.flipY = true;

    this.ship2.flipY = true;

    this.ship3.flipY = true;

  
    this.cursorKeys = this.input.keyboard.createCursorKeys();

    this.player.setScale(0.25);

    this.player.play("play");

    this.player.setCollideWorldBounds(true);

    this.spacebar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);

    this.projectiles = this.add.group();

    /************  player section end ******************** */

    this.physics.add.collider(this.projectiles, this.powerUps, function(projectile, powerUp) {

        projectile.destroy();

    });

    this.physics.add.overlap(this.player, this.powerUps, this.pickPowerUp, null, this);

    this.physics.add.overlap(this.player, this.ennemies, this.hurtPlayer, null, this);

    this.physics.add.overlap(this.projectiles, this.ennemies, this.hitEnemy, null, this);

    this.physics.add.collider(this.ufo, this.player, this.playerHitUfo, null, this);

    this.physics.add.collider(this.projectiles, this.ufo, this.hitUfo, null, this);

}

/* --------------------------- end create section ---------------------------*/

/* ------------------------- UPDATE SECTION -------------------------*/

update() {

    this.moveShip(this.ship1, 1);

    this.moveShip(this.ship2, 2);

    this.moveShip(this.ship3, 3);

    this.background.tilePositionY -= 0.5;

    this.planet.tilePositionY -= 0.5;

    this.stars.tilePositionY -= 0.5;

    this.movePlayer();

    if (Phaser.Input.Keyboard.JustDown(this.spacebar)) {

        if (this.player.active) {

            this.shootBeam();

        }

    }

    for (var i = 0; i < this.projectiles.getChildren().length; i++) {

        var beam = this.projectiles.getChildren()[i];

        beam.update();

    }

    if (this.score > 50) {

        this.ufo.setActive(true);

        this.ufo.setVisible(true);

        this.ufo.setVelocityX(100);
    }

}

What is in hitUfo() and playerHitUfo()?

@samme it’s function i have create. hitUfo it’s when a beam hit the ufo. playerHitUfo it’s for when the player hit the ufo. in one word, ufo is an enemy spaceship.

hitUfo(projectile, enemy) {

    var explosion = new Explosion(this, enemy.x, enemy.y);

    projectile.destroy();

    this.ufoDisappear(enemy)

    this.score += 30;

    var scoreFormated = this.zeroPad(this.score, 6);

    this.scoreLabel.text = "SCORE " + scoreFormated;

    this.explosionSound.play();

    this.ufo.destroy(true);

}

playerHitUfo(player) {

    if (this.player.alpha < 1) {

        return;

    };

    var boom = new Boom(this, player.x, player.y);

    player.disableBody(true, true); // fait disparaitre le vaisseau quand il touché

    this.time.addEvent({

        delay: 1000,

        callback: this.resetPlayer,

        callbackScope: this,

        loop: false

    });

    this.explosionSound.play();

Is it possible this.ufo.destroy() is called too early somehow?

@samme i try to delete this.ufo.destroy from the function hitUfo and yes now setVelocityX works but now i have another issue. when i shot the ufo. ufo is bouncing. so maybe, i have to find another way to destroy the ufo. but thank you for answering and taking the time to look at my code.