ParticleEffectPool.obtain() not resetting particles

| | August 6, 2015

I want to use ParticleEffectPools in libGDX but I’m running into an odd issue where the effects don’t seem to be reset when obtained by the pool. The code below looks for an existing ParticleEffectPool for the named effect, creates one if necessary, and then obtains a particle effect for the newly created Actor. The particle effect should be reset in the obtain function (if you look at the source it does so). What happens is the first effect works as expected but every subsequent one flashes very briefly on the screen as if its run out of duration.

public class ParticleEffectActor extends Actor {

    private static final Map<String, ParticleEffectPool> ParticlePools = new HashMap<>();
    private final PooledEffect effect;

    public ParticleEffectActor(String effectName, float rotation) {
        if(!ParticlePools.containsKey(effectName)) {
            ParticleEffect particleEffect = new ParticleEffect();
            particleEffect.load(Gdx.files.internal("resources/particles/" + effectName + ".p"), Images.Atlas);
            ParticlePools.put(effectName, new ParticleEffectPool(particleEffect, 10, 50));
        }

        this.effect = ParticlePools.get(effectName).obtain();

        rotation -= 90f;
        for(ParticleEmitter emitter : effect.getEmitters()) {
            emitter.reset();
            ParticleEmitter.ScaledNumericValue angle = emitter.getAngle();
            angle.setLow(rotation - 35, rotation + 35);
            angle.setHigh(rotation - 35, rotation + 35);
        }
    }

    @Override
    public void draw (SpriteBatch batch, float parentAlpha) {
        effect.draw(batch);
    }

    @Override
    public void act (float delta) {
        super.act(delta);
        effect.setPosition(getX(), getY());
        effect.update(delta);
    }

    @Override
    public boolean remove() {
        effect.free();
        return super.remove();
    }
}

One Response to “ParticleEffectPool.obtain() not resetting particles”

  1. For what it’s worth, I was having this issue (or at least very similar) where when I reset my emitters, I no longer had any particle effects.

    My situation is I needed to shrink my particle effects to a dynamic size upon each reset, since I am re-using projectile objects, that happen to contain a projectile trail, and each time a projectile object is fired, it is given the projectile properties the turret desires.

    Anyways, turns out I had done:

    m_TrailEffect.getScale().setLow(size * m_TrailEffect.getScale().getLowMin(), size * m_TrailEffect.getScale().getLowMax());
    m_TrailEffect.getScale().setHigh(size * m_TrailEffect.getScale().getHighMin(), size * m_TrailEffect.getScale().getHighMax());
    

    So each time I reset my emitter, my scale was getting larger/smaller by a factor of ‘size’!

    To fix it, I stored the initial scale low min/max and high min/max right after load()’ing the emitter, and used those instead of getMin/getMax:

    m_TrailEffect.getScale().setLow(m_ScaleLowMin * size, m_ScaleLowMax * size);
    m_TrailEffect.getScale().setHigh(m_ScaleHighMin * size, m_ScaleHighMax * size);
    

Leave a Reply