2012-02-05 88 views
1

我感興趣的開發粒子引擎,我可以調用類似.createCollisionEffect(pos x, pos y, float duration);Andengine粒子碰撞的影響(安卓遊戲開發)

,發動機會造成顆粒的隨機定向骨刺指定的時間。我發現下面的代碼,但我想用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

只是爲了澄清:你不是在試圖製造與物體碰撞的粒子,對吧?你正在發生碰撞時發生的粒子效應。那是你正在嘗試做什麼? – 2012-02-08 19:34:55

+0

是的,你明白了! – Aziz 2012-02-09 04:50:54

回答

6

您在上面的代碼中具有基礎知識。下面是如何做的,你在找什麼:

  1. 創建particleEmitter
  2. 創建粒子系統
  3. 添加改性劑和初始化給你的粒子所需要的行爲。

而現在的祕密武器:

  1. 停止你的粒子系統:particleSystem.setParticlesSpawnEnabled(false);

  2. 在碰撞,移動你的粒子發射器,它應該使用發射粒子:particleEmitter.setCenter(xPosition , yPosition);

  3. 啓動TimerHandler以在產生粒子的時間結束時關閉粒子。 TimerHandler是一個像處理程序一樣工作的Andengine類,但暫停並繼續遊戲。

這應該做的!

+1

你可以給我一個TimerHandlers的例子或者一個好資源的鏈接嗎? – Aziz 2012-02-11 00:55:35

+1

這裏是我撿起來的地方: http://stackoverflow.com/questions/8271473/how-to-change-timerhandler-delay-in-andengine 我推薦原始問題發佈的代碼。有用。 – 2012-02-11 01:53:07

+0

另外本教程構建了一個使用TimerHander的andengine遊戲。 https://jimmaru.wordpress.com/2011/09/28/andengine-simple-android-game-tutorial/ – 2012-02-11 01:55:10