Search code examples
libgdxgame-engine

Particle Effect Changing Color at runtime


I need to change color of my particle effect color according to some user event in my game for that this is what i am doing:

 float temp[] = new float[4];
 temp[0] = 0.937f;
 temp[1] = 0.325f;
 temp[2] = 0.314f;
 pe.getEmitters().first().getTint().setColors(temp);
 pe.start();

and in render i am doing this:

pe.draw(batch, Gdx.graphics.getDeltaTime());

but unfortunately i am getting this error:

java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.badlogic.gdx.graphics.g2d.ParticleEmitter$GradientColorValue.getColor(ParticleEmitter.java:1313)
at com.badlogic.gdx.graphics.g2d.ParticleEmitter.activateParticle(ParticleEmitter.java:439)
at com.badlogic.gdx.graphics.g2d.ParticleEmitter.addParticle(ParticleEmitter.java:154)
at com.badlogic.gdx.graphics.g2d.ParticleEmitter.draw(ParticleEmitter.java:299)
at com.badlogic.gdx.graphics.g2d.ParticleEffect.draw(ParticleEffect.java:74)
at com.approduction.game.GameScreen.render(GameScreen.java:218)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:459)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1557)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1263)

i dont know what i am doing wrong, i read the documentation and has done everything according to it any help would be a savior... Thanks in advance..


Solution

  • Your float array has the wrong length.

    You actually don't need to create a new array. You can avoid this problem altogether by filling your colors into the array it already has like this:

     float temp[] = pe.getEmitters().first().getTint().getColors();
     temp[0] = 0.937f;
     temp[1] = 0.325f;
     temp[2] = 0.314f;
     pe.start();