Andengine粒子碰撞效果(Android游戏开发)

时间:2012-02-05 16:29:42

标签: android andengine particle-system

我有兴趣开发一个粒子引擎,我可以称之为.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;
}

有人可以提供一些指导吗?提前谢谢!

1 个答案:

答案 0 :(得分:6)

您在上面的代码中有基础知识。以下是如何做你想要的:

  1. 创建particleEmitter
  2. 创建粒子系统
  3. 添加修饰符和初始值设定项,为粒子提供所需的行为。
  4. 现在秘诀是:

    1. 停止您的粒子系统:particleSystem.setParticlesSpawnEnabled(false);

    2. 在碰撞时,使用以下方法将粒子发射器移动到应发射粒子的位置:particleEmitter.setCenter(xPosition , yPosition);

    3. 启动TimerHandler以在产生粒子的时间结束时关闭粒子。 TimerHandler是一个Andengine类,它像处理程序一样工作,但暂停和恢复游戏。

    4. 那应该做啊!