Last active
October 4, 2016 20:07
-
-
Save pixelhijack/b6559620d1303714c39722b6418d1d07 to your computer and use it in GitHub Desktop.
[Phaser.js] Extended / custom sprites subscribe to event
This file contains hidden or 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 Game(){ | |
/* | |
Then in your gamestate.create you add a Phaser.Signal | |
with which you can dispatch events: | |
*/ | |
var lizardman, | |
eventChannel = new Phaser.Signal(); | |
this.preload = function(){ }; | |
this.create = function(){ | |
lizardman = new Lizardman(this.game, 100, 50, 'lizardmanSpriteKey'); | |
}; | |
/* | |
You subrscribe the lizardman to the Phaser.Signal channel: | |
*/ | |
lizardman.listen(eventChannel, lizardman.onEventHappenz); | |
/* | |
Then in your gamestate.update you can just fire an event anywhere | |
and your lizardman can react: | |
*/ | |
this.update = function(){ | |
this.game.physics.arcade.collide(lizardman, enemies, function(){ | |
eventChannel.dispatch({ type: 'AN_ENEMY_ATTACKED_POOR_LIZARDMAN' }); | |
}.bind(this)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment