Jump to content

Custom Class for Particles


BeachBum
 Share

Recommended Posts

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

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

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 undefined

in 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

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

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...