粒子效果在运行时改变颜色

Particle Effect Changing Color at runtime

我需要根据游戏中的某些用户事件更改粒子效果颜色的颜色,因为这就是我正在做的事情:

 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();

在渲染中我这样做:

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

但不幸的是我收到了这个错误:

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)

我不知道我做错了什么,我阅读了文档并根据它做了所有的事情任何帮助都会是一个救世主...提前致谢..

您的浮点数组长度错误。

您实际上不需要创建新数组。您可以通过将颜色填充到已有的数组中来完全避免此问题,如下所示:

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