Jump to content

what is the best way to handle multiples objects?


mmVac
 Share

Recommended Posts

Hello,

What is the best way to handle multiples objects? I have created 5 of them, but I can work around with only one. What should I do to handle all objects in my 'moveEnemy' functions?

 

  1.  
    //scene.js
     
    create() {
        for (var i = 0; i < 5; i++) {
          this.enemy = this.physics.add.existing(new Enemy(this)); //I've messed up here.
    }
     
    update() {
        this.moveEnemy(this.enemy);
    }
     
    moveEnemy(enemy) {
        if (enemy.y > config.height) {
          this.resetPos(this.enemy);
        }
    }
     
    resetPos(enemy) {
        this.enemy.y = Phaser.Math.Between(-200, 0);
        this.enemy.x = Phaser.Math.Between(0, config.width);
    }
     
     
     
    //enemy.js
     
    class Enemy extends Phaser.GameObjects.Sprite {
      constructor(scene) {
        var x = Phaser.Math.Between(0, config.width);
        var y = Phaser.Math.Between(-500, -50);
        super(scene, x, y, "enemy");
     
        this.scene.add.existing(this);
     
        this.scene.physics.world.enableBody(this);
        this.body.velocity.y = 100;
    }
     
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...