Created
April 7, 2014 20:41
-
-
Save ada-lovecraft/10048482 to your computer and use it in GitHub Desktop.
Pipe Prefabs
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
'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; |
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
'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; |
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
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