kuka93 Posted September 21, 2016 Share Posted September 21, 2016 Hi, I'm new with Phaser and I'm developing a small platformer game. I'm creating a group of enemies that should open fire every 1-2 seconds, but the problem is that only one of those shoots. In the create method my code is: // Creating bullets for robot this.lasersRobot = this.game.add.group(); this.lasersRobot.enableBody = true; this.lasersRobot.physicsBodyType = Phaser.Physics.ARCADE; this.lasersRobot.createMultiple(bulletsRobot, 'bulletRobot'); this.lasersRobot.callAll('events.onOutOfBounds.add', 'events.onOutOfBounds', this.resetLaserRobot); this.lasersRobot.callAll('anchor.setTo', 'anchor', 0.5, 1.0); this.lasersRobot.setAll('checkWorldBounds', true); // Creating the group of enemies this.enemiesRobot = this.game.add.group(); this.map.createFromObjects('ObjectLayer', 149, 'enemyRobot', 0, true, false, this.enemiesRobot); this.enemiesRobot.physicsBodyType = Phaser.Physics.ARCADE; this.enemiesRobot.forEach(this.setupEnemiesRobot,this); then, in the update method, the snippet of the code for the enemies' shooting is: if (this.game.time.now > nextFire){ nextFire = this.game.time.now + fireRate; var laser = this.lasersRobot.getFirstExists(false); if (laser) { laser.reset(enX-10, enY); laser.body.velocity.x = -300; this.shootSound.play(); } } I have 8 enemies, but only the first one opens fire every 1 second.. Can anyone help me? Link to comment Share on other sites More sharing options...
samme Posted September 21, 2016 Share Posted September 21, 2016 Don't you want something like if (this.game.time.now > nextFire){ nextFire = this.game.time.now + fireRate; this.enemiesRobot.forEachAlive(function (enemy){ var laser = this.lasersRobot.getFirstExists(false); /* shoot … */ }, this); } drhayes 1 Link to comment Share on other sites More sharing options...
kuka93 Posted September 22, 2016 Author Share Posted September 22, 2016 Thank you! Now it works Link to comment Share on other sites More sharing options...
Recommended Posts