Skip to content

Instantly share code, notes, and snippets.

@videlais
Last active Aug 29, 2015
Embed
What would you like to do?
Blader subclass of Phaser.Sprite for #LOWREZJAM
function Blader(game, x, y, cacheKey) {
// Call the Phaser.Sprite constructor
Phaser.Sprite.call(this, game, x, y, cacheKey);
// Enable physics
this.game.physics.enable(this);
// Set the (physics) body to be immovable
this.immovable = true;
// Set the initial target coordinates
this.target = new Phaser.Point(0, 0);
// Set the 'fly' animation
this.animations.add('fly', [0, 1], 5, true);
// Set the velocity to chase another sprite
this.speed = 90;
// The 'explosionGroup' for spawning explosions
this.explosionGroup = new ExplosionGroup(this.game);
// Set that this sprite is 'alive'
this.alive = true;
// Set the distance at which a Blader should
// destory() itself.
this.killDistance = this.width * 24;
}
Blader.prototype = Object.create(Phaser.Sprite.prototype);
Blader.prototype.constructor = Blader;
/*
* This is called from the main game loop like the following:
* this.bladerGroup.callAll('updateTarget', null, this.player.x, this.player.y);
*
* It updates the 'target' position for all Bladers in the group
*
*/
Blader.prototype.updateTarget = function(x, y) {
this.target.x = x;
this.target.y = y;
};
Blader.prototype.update = function() {
// If the 'target.x' is still 0, don't do anything
if (this.target.x !== 0) {
// At some point, the 'target' has been updated, so
// we need to move toward the target at the 'speed'
this.game.physics.arcade.moveToXY(this,
this.target.x, this.target.y, this.speed);
// Since we are moving, play the 'fly' animation
this.play('fly');
// If the target is greater than 24 * width OR 24 * height away,
// go ahead and destory yourself. (If they are that
// far away, a Blader cannot ever catch up.)
if ((this.target.x - this.x) > this.killDistance ||
(this.target.y - this.y) > this.killDistance) {
Phaser.Sprite.prototype.destroy.call(this);
}
}
};
Blader.prototype.kill = function() {
// Create an explosion at this position
this.explosionGroup.explode(this.x, this.y);
// kill() this sprite by calling Phaser.Sprite's
// own kill function and passing 'this'
Phaser.Sprite.prototype.kill.call(this);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment