2013-03-02 74 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); 

相反,您可以跳過池並使煙霧粒子效果循環(連續)。這可能不會讓你確切地找到你想要的東西,並且可能被認爲是一個混亂,但可能在一個捏。