Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pixelhijack/b6559620d1303714c39722b6418d1d07 to your computer and use it in GitHub Desktop.
Save pixelhijack/b6559620d1303714c39722b6418d1d07 to your computer and use it in GitHub Desktop.
[Phaser.js] Extended / custom sprites subscribe to event
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