Skip to content

Instantly share code, notes, and snippets.

@videlais
Created May 31, 2014
Embed
What would you like to do?
Bigeye subclass of Phaser.Sprite for #LOWREZJAM 2014
function Bigeye(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.body.immovable = true;
// Center the anchor
this.anchor.setTo(0.5, 0.5);
// The 'triggerBox'
// If another sprite is within this rectangle, jump at them
this.triggerBox = new Phaser.Rectangle(
x - (this.width * 5),
y - (this.height * 5),
this.width * 5.5,
this.height * 5.5);
// The 'explosionGroup' for spawning explosions
this.explosionGroup = new ExplosionGroup(this.game);
// Set that this sprite is 'alive'
this.alive = true;
// Set the gravity
this.body.gravity.y = 825;
// Set health
this.health = 12;
// The jumping rate (in milliseconds)
this.jumpingRate = 1400;
// Time of next jump
this.nextJump = 0;
// Always moving left
this.body.velocity.x = -12;
}
Bigeye.prototype = Object.create(Phaser.Sprite.prototype);
Bigeye.prototype.constructor = Bigeye;
Bigeye.prototype.jump = function() {
// Every (jumping rate), move up (negative velocity)
if (this.game.time.now > this.nextJump) {
this.nextJump = this.game.time.now + this.jumpingRate;
this.body.velocity.y = -350;
}
};
Bigeye.prototype.update = function() {
// If this is !alive (dead), don't update anything
if (this.alive) {
// Adjust the triggerBox coordinates
this.triggerBox.x = this.x - (this.width * 5);
this.triggerBox.y = this.y - (this.height * 5);
this.triggerBox.width = this.width * 5.5;
this.triggerBox.height = this.height * 5.5;
}
};
Bigeye.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);
};
GameState.prototype.create = function() {
...
this.bigeye = new Bigeye(this.game, 167, 64, 'bigeye');
this.game.add.existing(this.bigeye);
...
};
GameState.prototype.update = function() {
...
this.game.physics.arcade.collide(this.bigeye, this.layer);
this.game.physics.arcade.collide(this.player, this.bigeye,
function(player) {
player.damage(1);
player.blink();
});
...
if (this.bigeye.triggerBox.contains(this.player.x, this.player.y)) {
this.bigeye.jump();
}
...
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment