-
-
Save videlais/0f62776c904e0d544313 to your computer and use it in GitHub Desktop.
ExplosionGroup subclass of Phaser.Group for #LOWREZJAM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ExplosionGroup(game) { | |
// Call the Phaser.Group constructor | |
Phaser.Group.call(this, game); | |
// Have the game add this group | |
this.game.add.existing(this); | |
} | |
ExplosionGroup.prototype = Object.create(Phaser.Group.prototype); | |
ExplosionGroup.prototype.constructor = ExplosionGroup; | |
ExplosionGroup.prototype.explode = function(x, y) { | |
// Get the first 'dead' member of the group | |
var explosion = this.getFirstDead(); | |
// If 'explosion' is null, there weren't any 'dead' memebers | |
if (explosion === null) { | |
// Create a sprite using the 'explosion' cache key | |
explosion = this.game.add.sprite(0, 0, 'explosion'); | |
// Center its anchor | |
explosion.anchor.setTo(0.5, 0.5); | |
// Create an animation based on the 'explosion' cache key | |
// and label it 'boom' for later playing. | |
// (We also need to save a reference to the create animation.) | |
var animation = explosion.animations.add('boom', [0, 1, 2, 3], 50, false); | |
// Tell the animation to kill() itself when done | |
animation.killOnComplete = true; | |
// All the sprite + animation to the group | |
this.add(explosion); | |
} | |
// If there was a 'dead' member in the group, revive it. | |
// Otherwise, 'revive' the new member. | |
explosion.revive(); | |
// Reset its position to those passed into this function | |
explosion.reset(x, y); | |
// Pick a random angle for the sprite (and thus animation) | |
explosion.angle = this.game.rnd.integerInRange(0, 360); | |
// Play the 'boom' animation. | |
// (When it is done, it will kill the animation.) | |
explosion.animations.play('boom'); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment