2013-03-02 71 views
2

我是LibGDX的新手......我试图将粒子效果附加到子弹对象上。 我有球员,射出多颗子弹,我想添加一些烟雾和火焰后发射子弹。libgdx粒子编辑器多重用法

问题是我每次都得不到相同的效果。 最初的第一个子弹效果看起来像我想要的,每一个子弹的后面都有一条较短的线索。就像没有颗粒可以绘制一样。

我想如果可能的话使用一个粒子发射器对象。不希望每个子弹对象都有多个实例。

我试图使用reset()方法,绘制每个项目符号后,但它看起来不一样了。只有第一个是好的,其他的都不好看一点。

有没有办法做到这一点?

帮助!

这是代码片段:

初始化:

bulletTexture = new Texture("data/bullet.png"); 
    bulletTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 

    // Load particles 
    bulletFire = new ParticleEmitter(); 

    try { 
     bulletFire.load(Gdx.files.internal("data/bullet_fire_5").reader(2048)); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    // Load particle texture 
    bulletFireTexture = new Texture("data/fire.png"); 
    bulletFireTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear); 

    // Attach particle sprite to particle emitter 
    Sprite particleSprite = new Sprite(bulletFireTexture); 
    bulletFire.setSprite(particleSprite); 
    bulletFire.start(); 

在渲染方法:

// Draw bullets 

    bulletIterator = bullets.iterator(); 

    while (bulletIterator.hasNext()) { 

     bullet = bulletIterator.next(); 

     bulletFire.setPosition(bullet.getPosition().x + bullet.getWidth()/2, 
       bullet.getPosition().y + bullet.getHeight()/2); 
     setParticleRotation(bullet); 


     batch.draw(bulletTexture, bullet.getPosition().x, 
       bullet.getPosition().y, bullet.getWidth()/2, 
       bullet.getHeight()/2, bullet.getWidth(), 
       bullet.getHeight(), 1, 1, bullet.getRotation(), 0, 0, 
       bulletTexture.getWidth(), bulletTexture.getHeight(), false, 
       false); 


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

    } 
+0

我觉得效果在用完之后“会耗尽”(它在编辑器中有一个“持续时间”)。有一些像'if(bulletFire.isComplete()){bulletFire.start(); }'帮忙? – 2013-03-03 00:22:52

+0

不,从开始看起来好一点......但是在几发子弹之后,同样的事情发生了。我应该使用粒子发射器阵列吗?不知道这是否是必要的。 – Veljko 2013-03-03 10:58:45

+0

我已经添加了Array 这种方式,它适用于每个子弹。我的问题是这样使用它有效吗? – Veljko 2013-03-05 10:32:04

回答

2

正如注释状态,问题是,你正在使用一个效果多颗子弹。我怀疑它被设置为不连续的,所以持续时间有限。随着时间的推移,效果用完了。

我建议为效果创建一个粒子效果池,将obtain()从/ free()添加到池中。为每个子弹附加一个效果。这允许您在限制垃圾回收的同时运行多个效果(由于池),并避免为每个新效果加载文件。该池使用每次调用obtain()时复制的模板创建。请在free()的电话中注明PooledEffect。

import com.badlogic.gdx.graphics.g2d.ParticleEffect; 
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool; 
import com.badlogic.gdx.graphics.g2d.ParticleEffectPool.PooledEffect; 

... 

ParticleEffect template = new ParticleEffect(); 
template.load(Gdx.files.internal("data/particle/bigexplosion.p"), 
      ..texture..); 
ParticleEffectPool bigExplosionPool = new ParticleEffectPool(template, 0, 20); 

... 

ParticleEffect particle = bigExplosionPool.obtain(); 

... // Use the effect, and once it is done running, clean it up 

bigExplosionPool.free((PooledEffect) particle); 

相反,您可以跳过池并使烟雾粒子效果循环(连续)。这可能不会让你确切地找到你想要的东西,并且可能被认为是一个混乱,但可能在一个捏。