Jump to content

Asteroids - how to revive from a sprite group?


frokenstein
 Share

Recommended Posts

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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