frokenstein Posted November 16, 2014 Share Posted November 16, 2014 I created a small space game. I have my asteroid group and it is set to check for out of bounds:this.asteroids = this.game.add.group();this.asteroids.setAll('anchor.x', 0.5);this.asteroids.setAll('anchor.y', 0.5);this.asteroids.setAll('checkWorldBounds', true);I have this function that I call on an interval until I reach the max number of asteroids:createAsteroid: function () {if (this.asteroidCount < this.TOTALASTEROIDS) {var start = this.game.rnd.integerInRange(1, 4);switch (start) {case 1:var x = this.game.world.bounds.width;var y = this.game.rnd.integerInRange(0, this.game.world.bounds.height);var direction = 1;break;case 2:var x = this.game.world.bounds.x;var y = this.game.rnd.integerInRange(0, this.game.world.bounds.height);var direction = 2;break;case 3:var x = this.game.rnd.integerInRange(0, this.game.world.bounds.width);var y = this.game.world.bounds.height;var direction = 3;break;case 4:var x = this.game.rnd.integerInRange(0, this.game.world.bounds.width);var y = this.game.world.bounds.y;var direction = 4;break;}var asteroid = this.asteroids.getFirstExists(false);if (!asteroid) {asteroid = new Asteroid(this.game, x, y, direction);asteroid.events.onOutOfBounds.add(this.killAsteroid, asteroid);this.asteroids.add(asteroid);}asteroid.reset(x, y);asteroid.revive();this.asteroidCount++;}}As you can see, I have bound a function to each asteroid for the out of bounds event:asteroid.events.onOutOfBounds.add(this.killAsteroid, asteroid);This is the kill function, which kills the asteroid, console logs it, and reduces the total asteroid count so another can be created:// Asteroid out of boundskillAsteroid: function(asteroid) {asteroid.kill();console.log("asteroid destroyed");this.asteroidCount--;},The problem is that this event is never triggered. I have properly set the asteroids group to check if they're out of bounds, yet the killAsteroid() function is never called. What this means is I have one wave of asteroids that fly through the canvas, followed by a whole lot of nothing. My desire is to revive asteroids that have left the game by resetting them where they originally started and with the same velocity. But no revival ever occurs. What am I doing wrong? Link to comment Share on other sites More sharing options...
rvizcaino Posted November 17, 2014 Share Posted November 17, 2014 You are asking for the getFirstExists in group, but at that moment you have zero elements in the group. I recommend using group.createMultiple function just after creating the group, with this function you can initialize all the asteroids sprites needed. Link to comment Share on other sites More sharing options...
frokenstein Posted November 19, 2014 Author Share Posted November 19, 2014 Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts