BeachBum Posted September 19, 2014 Share Posted September 19, 2014 I've been trying to figure out who to create a custom particle class. I get the concept of prototype to some extent but I'm not sure how to use that with the emitter.particleClass property. Can someone show a really brief example of how to extend the Particle class with an extra property and or member? Thanks! Link to comment Share on other sites More sharing options...
Dumtard Posted September 19, 2014 Share Posted September 19, 2014 Here is a simple example of how to extend a class.var MyClass = function() { BaseClass.call(this); this.variable = null // Extra property};MyClass.prototype = Object.create(BaseClass.prototype);MyClass.prototype.constructor = MyClass;MyClass.prototype.func = function() { }; // Extra function Link to comment Share on other sites More sharing options...
BeachBum Posted September 19, 2014 Author Share Posted September 19, 2014 Thanks, That's getting me started but I must be missing something. Can you take a look at this:var MyParticle = function() { Phaser.Particle.call(this); this.size = null;};MyParticle.prototype = Object.create(Phaser.Particle.prototype);MyParticle.prototype.constructor = MyParticle;MyParticle.prototype.onEmit = function() { this.size = this.scale.x;}my issue with using this class for my emitter.particleClass is that I get an error in phaser.js:Uncaught TypeError: Cannot read property 'physics' of undefinedin the preupdate function It makes me think I'm hiding the other functionality of the Particle class by my additions.Any ideas where I've gone wrong? Link to comment Share on other sites More sharing options...
BeachBum Posted September 19, 2014 Author Share Posted September 19, 2014 it cut off half of my comment but I eventually figured out how to edit it, hopefully nobody saw it when it was a mess Link to comment Share on other sites More sharing options...
crysfel Posted September 19, 2014 Share Posted September 19, 2014 I think you need to pass the game instance, look at the code (line 15) http://docs.phaser.io/Particles.js.html#sunlight-1-line-15 is expecting the game instance and you are not sending it, try to do this: var MyParticle = function(game) { Phaser.Particle.call(this,game); this.size = null;};Regards Link to comment Share on other sites More sharing options...
BeachBum Posted September 19, 2014 Author Share Posted September 19, 2014 Getting close guys! Now it totally works but the sprite doesn't show. I can see the particles interacting with other game objects as they should but they're invisible.I wondered if I was passing in a blank reference to a game by simply adding in the game parameter. I tried adding a reference to the game when I assigned it: this.greenEmitter.particleClass = MyParticle(this); but that led to an error in the Sprite class.Anything else I could be missing? Link to comment Share on other sites More sharing options...
BeachBum Posted September 19, 2014 Author Share Posted September 19, 2014 I'm not sure this is the best solution but it worked. I added a loadTexture call in to the onEmit event handler like so:var MyParticle = function(game) { Phaser.Particle.call(this, game); this.size = null;};MyParticle.prototype = Object.create(Phaser.Particle.prototype);MyParticle.prototype.constructor = MyParticle;MyParticle.prototype.onEmit = function() { this.size = this.scale.x; this.loadTexture('greenParticle');} works like a charm, thanks for all the help! Link to comment Share on other sites More sharing options...
Recommended Posts