Jump to content

Trouble adding Animations to Particles


Recommended Posts

Hey all, I currently have a special custom particle sub class, called Powerup (Using ES6 and Phaser 2.6.1). It looks like the following, truncated for brevity

export default class Powerup extends Phaser.Particle {

  constructor(game, state, position, key, value) {
    super(game, position.x, position.y, key);

    this.animations.add('anim', [0, 1, 2, 3, 4, 5], 24, true);
    this.animations.play('anim', true);
.. minor other functions ..

In this class I'm adding an animation above in the normal fashion, above.

I then have a particle emitter subclass, called Spawner:

import Powerup from '../objects/Powerup';

export default class Spawner extends Phaser.Particles.Arcade.Emitter {

  constructor(game, position, config) {
    console.log('Starting Spawner');

    super(game, position.x, position.y, config.total);

    //Enable physics
    this.enableBody = config.collide || true;

    //Initial Position
    this.position = position;

    //Give some speed
    this.minParticleSpeed.set(0, 300);
    this.maxParticleSpeed.set(0, 400);

    //Create particles
    this.particleClass = Powerup;


When I add my particle emitter to the game with 

this.game.spawner =  new Spawner(this.game, spawnerPosition, spawnerConfig);
this.game.spawner.width = this.game.width;


I get the following error:

Uncaught TypeError: Cannot read property 'getFrameIndexes' of null

Which traces back to this line in the Powerup class:

this.animations.add('anim', [0, 1, 2, 3, 4, 5], 24, true);

According to the Docs, this should work. I can't figure out why getFrameIndexes is null, when they are specified in the constructor for each Particle?

Link to comment
Share on other sites

I figured this out.

It's because .makeParticles doesn't accept custom properties to your particle object. So one has to override makeParticles in the emitter subclass to do so, in my case that meant the key that links to the spritesheet asset wasn't passed.

My use case was to have a standalone sprite-based powerup class, then have certain enemies "explode" in instances of that same powerup class e.g Like when sonic gets hit and his rings pop out of him.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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