swissnetizen Posted January 6, 2015 Share Posted January 6, 2015 Hi, I'm having some trouble figuring out how we would dispatch events from a sprite.I've got a constructor called Player which inherits from Creature (which inherits from Phaser.Sprite).I want to be able to dispatch an event from player in a manner similar to this:var player = new Player([…]);player.bind("death", function ()[…] Would my approach be possible in Phaser? Is the approach correct?The alternative idea would be to check wheather the Player is dead in the update function but that seems wrong. I was also wondering if there is an updateEvent(Fires every frame) ?I have a feeling my whole approach to the problem is wrong. Check line 25 to 28 of pastebin(http://pastebin.com/hXjVS3MA) Link to comment Share on other sites More sharing options...
spencerTL Posted January 6, 2015 Share Posted January 6, 2015 If you want to act when player is killed then you can listen for the sprite.events.onKilled event after you kill player with sprite.kill(); As you are inheriting from Phaser.sprite this should be available to your Player class. You could also add a custom event listener. This onKilled event is dispatched automatically so just listen for it with this.player.events.onKilled.add(yourDeathFunction, this);Param 1 is the function to run on the death of player, param 2 is the context. http://docs.phaser.io/Phaser.Sprite.html#kill Each sprite also has an update function that you can override to your own ends. Just create an update function in your class and do what you want in it - although remember this should only have stiff in it that you really must do every frame if you want to keep performance up. http://docs.phaser.io/Phaser.Sprite.html#update (I haven't checked your code as I'm on a phone so apologies if this is at odds with what you've written there). Link to comment Share on other sites More sharing options...
swissnetizen Posted January 7, 2015 Author Share Posted January 7, 2015 Unfortunately the update function is not working. I've currently got something similar: this.update = function () { this.checkControls(); console.log("HELLO"); }.bind(this);And I do not see HELLO at all!I've also got an console.log in my state's update function and it is firing. Link to comment Share on other sites More sharing options...
spencerTL Posted January 7, 2015 Share Posted January 7, 2015 My experience with JS is not such that I have a broad knowledge of the different code structures available. The following code shows the essentials of how I extend sprite that works for me. You're clearly using a different structure and you may want to stick with that but it is outside my knowledge. As a side note, I've had to use bind when working with other frameworks but never with Phaser as I've always been able to pass the scope within its functions. This may be due to my pretty much adopting the style that Phaser uses in its templates rather than a feature of the framework itself but I see it as a massive plus. I hate scope in JS, and the prototype system! Anyway, if it is any use, this is my barebones structure for extending sprite, almost certainly I've taken it in essence from a Phaser example or template: Player = function (game,x,y,key,frame,image) { Phaser.Sprite.call(this, game, x, y,key,frame,image); Phaser.Sprite.call(this,game);};Player.prototype = Object.create(Phaser.Sprite.prototype);Player.prototype.constructor = Player;Player.prototype.update = function() { console.log('this is updating')}; Link to comment Share on other sites More sharing options...
swissnetizen Posted January 8, 2015 Author Share Posted January 8, 2015 Solved the problem, I'd forget to apply my creature constructor in the player constructor. Link to comment Share on other sites More sharing options...
Recommended Posts