setCollideWorldBounds not working

Hi, i’m practicing my coding skill in Javascript and Phaser3. For this i try to do a space shooter. I would like an enemy ship not to leave the screen (update section. it’s in the condition " if (this.score > 50) ". I try to use the fonction setCollideWorldBounds. My program keeps telling me that setCollideWorldBounds is not a function and i don’t understand why.

What is strange is that the function setCollideWorldBounds works with my player.

Someone can help me please ?

(sorry for my bad english)

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.add.image(config.width / 30, config.height / 2, "enemy");

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

    this.ennemies.add(this.ship1);

    this.ennemies.add(this.ship2);

    this.ennemies.add(this.ship3);

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

    this.spaceship.add(this.ufo);

    this.spaceship.setActive(false);

    this.spaceship.setVisible(false);

    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;

   

    /* --------------------------player *********************************/

    this.player = this.physics.add.sprite(config.width / 2 - 8, config.height - 64, "player");

    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.overlap(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.spaceship.setActive(true);

        this.spaceship.setVisible(true);

        //this.moveShip(this.ufo, 1);

        this.physics.add.overlap(this.spaceship, this.player, this.playerHitUfo, null, this);

        this.moveUfo(this.spaceship, 1);

        this.spaceship.setVelocityX(30);

this.spaceship.setCollideWorldBounds(true);

        //this.Firebullet();

     

    }

}
1 Like

setCollideWorldBounds works on a body, not an entire group.

1 Like

had the same problem yesterday. thx

@Milton thanks for your answer. i will try that.