我有兴趣开发一个粒子引擎,我可以称之为.createCollisionEffect(pos x, pos y, float duration);
引擎将在指定的持续时间内创建一个随机方向的粒子杂散。我发现以下代码,但我想使用3种不同的纹理,所以它随机选择一个,但我不知道如何管理时间和3种不同的纹理:我找到了以下代码:
public ParticleSystem createParticleSystem(final TextureRegion textureRegion) {
//X & Y for the particles to spawn at.
final float particlesXSpawn = 400;
final float particlesYSpawn = 300;
//Max & min rate are the maximum particles per second and the minimum particles per second.
final float maxRate = 10;
final float minRate = 5;
//This variable determines the maximum particles in the particle system.
final int maxParticles = 100;
//Particle emitter which will set all of the particles at a ertain point when they are initialized.
final PointParticleEmitter pointParticleEmtitter = new PointParticleEmitter(particlesXSpawn, particlesYSpawn);
//Creating the particle system.
final ParticleSystem particleSystem = new ParticleSystem(pointParticleEmtitter, maxRate, minRate, maxParticles, textureRegion);
//And now, lets create the initiallizers and modifiers.
//Velocity initiallizer - will pick a random velocity from -20 to 20 on the x & y axes. Play around with this value.
particleSystem.addParticleInitializer(new VelocityInitializer(-20, 20, -20, 20));
//Acceleration initializer - gives all the particles the earth gravity (so they accelerate down).
particleSystem.addParticleInitializer(new GravityInitializer());
//And now, adding an alpha modifier, so particles slowly fade out. This makes a particle go from alpha = 1 to alpha = 0 in 3 seconds, starting exactly when the particle is spawned.
particleSystem.addParticleModifier((IParticleModifier) new AlphaModifier(3, 1, 0));
//Lastly, expire modifier. Make particles die after 3 seconds - their alpha reached 0.
particleSystem.addParticleModifier(new ExpireModifier(3));
return particleSystem;
}
有人可以提供一些指导吗?提前谢谢!
答案 0 :(得分:6)
您在上面的代码中有基础知识。以下是如何做你想要的:
现在秘诀是:
停止您的粒子系统:particleSystem.setParticlesSpawnEnabled(false);
在碰撞时,使用以下方法将粒子发射器移动到应发射粒子的位置:particleEmitter.setCenter(xPosition , yPosition);
启动TimerHandler以在产生粒子的时间结束时关闭粒子。 TimerHandler是一个Andengine类,它像处理程序一样工作,但暂停和恢复游戏。
那应该做啊!