Skip to content

Instantly share code, notes, and snippets.

@ada-lovecraft
Created April 7, 2014 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ada-lovecraft/10048482 to your computer and use it in GitHub Desktop.
Save ada-lovecraft/10048482 to your computer and use it in GitHub Desktop.
Pipe Prefabs
'use strict';
var nameCounter = 0;
var Pipe = function(game, x, y, frame) {
Phaser.Sprite.call(this, game, x, y, 'pipe', frame);
this.anchor.setTo(0.5, 0.5);
this.game.physics.arcade.enableBody(this);
this.body.allowGravity = false;
this.body.immovable = true;
this.name = 'pipe-' + nameCounter;
nameCounter++;
};
Pipe.prototype = Object.create(Phaser.Sprite.prototype);
Pipe.prototype.constructor = Pipe;
Pipe.prototype.update = function() {
console.log(this.name, this.x);
// write your prefab's specific update code here
};
module.exports = Pipe;
'use strict';
var Pipe = require('./pipe');
var PipeGroup = function(game, parent) {
Phaser.Group.call(this, game, parent);
this.topPipe = new Pipe(this.game, 0, 0, 0);
this.bottomPipe = new Pipe(this.game, 0, 440, 1);
this.add(this.topPipe);
this.add(this.bottomPipe);
this.hasScored = false;
this.scoreLine = new Phaser.Line(0,0,100,this.game.height);
this.setAll('body.velocity.x', -100);
};
PipeGroup.prototype = Object.create(Phaser.Group.prototype);
PipeGroup.prototype.constructor = PipeGroup;
PipeGroup.prototype.update = function() {
if(!this.topPipe.inWorld && this.exists) {
this.exists = false;
}
this.scoreLine.start = new Phaser.Point(this.topPipe.world.x, 0);
this.scoreLine.end = new Phaser.Point(this.topPipe.world.x,this.game.height);
};
PipeGroup.prototype.reset = function(x, y) {
this.topPipe.reset(0,0);
this.bottomPipe.reset(0,440);
this.x = x;
this.y = y;
this.setAll('body.velocity.x', -100);
this.hasScored = false;
this.exists = true;
};
PipeGroup.prototype.stop = function() {
};
module.exports = PipeGroup;
update: function() {
// enable collisions between the bird and the ground
this.game.physics.arcade.collide(this.bird, this.ground, this.deathHandler, null, this);
this.pipes.forEachExists(function(pipeGroup) {
this.game.physics.arcade.collide(this.bird, pipeGroup, this.deathHandler, null, this);
}, this);
this.checkScoreLines();
},
checkScoreLines: function() {
this.pipes.forEach(function(pipeGroup) {
if(pipeGroup) {
var p = this.scoreLine.intersects(pipeGroup.scoreLine, true);
if(p && !pipeGroup.hasScored) {
console.debug('score!');
pipeGroup.hasScored = true;
}
}
}, this);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment