我有兴趣开发一个粒子引擎,我可以调用类似 .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;
}
谁能提供一些指导?提前致谢!
最佳答案
您在上面的代码中已经掌握了基础知识。以下是如何执行您正在寻找的操作:
- 创建粒子发射器
- 创建粒子系统
- 添加修改器和初始化器,使您的粒子具有您想要的行为。
现在是秘诀:
停止粒子系统:
particleSystem.setParticlesSpawnEnabled(false);
在发生碰撞时,使用以下命令将粒子发射器移动到应该发射粒子的位置:
particleEmitter.setCenter(xPosition , yPosition);
启动 TimerHandler 以在生成粒子的时间结束时关闭粒子。 TimerHandler 是一个 Andengine 类,其工作方式类似于处理程序,但会随着游戏暂停和继续。
你应该这样做!
关于android - Andengine 粒子碰撞效果 (Android Game Dev),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9150867/