【问题标题】:libgdx particle editor multiple usagelibgdx 粒子编辑器多种用法
【发布时间】:2013-03-02 18:55:52
【问题描述】:

我是 LibGDX 的新手...我正在尝试将粒子效果附加到子弹对象。 我有播放器,它会发射多发子弹,我想在发射的子弹之后添加一些烟雾和火焰。

问题是我每次都没有得到相同的效果。 最初第一个子弹效果看起来像我想要的,每个其他子弹后面都有一个较短的轨迹。就像没有要绘制的粒子一样。

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

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

有没有办法做到这一点?

救命!

这是代码sn-p:

初始化:

    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());

    }

【问题讨论】:

  • 我认为效果会在一段时间后“耗尽”(它应该在编辑器中有一个“持续时间”)。 if (bulletFire.isComplete()) { bulletFire.start(); } 之类的东西有帮助吗?
  • 不,刚开始看起来好一点……但在几颗子弹之后,同样的事情发生了。我应该使用粒子发射器阵列吗?不确定这是否有必要。
  • 我添加了 Array 这样它对每个子弹都有效。我的问题是这样使用它是否有效?

标签: java libgdx effects


【解决方案1】:

正如 cmets 所述,问题在于您对多个子弹使用单一效果。我怀疑它设置为非连续的,因此持续时间有限。时间一长,效果就用完了。

我建议为效果创建一个粒子效果池,并将 obtain() 从 / free() 到池。为每个子弹附加一个效果。这允许您运行多个效果,同时限制垃圾收集(由于池化)以及避免为每个新效果加载 .p 文件。该池是使用每次调用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);

相反,您可以跳过水池并使烟雾粒子效果循环(连续)。这可能无法为您提供所需的确切信息,并且可能被视为杂牌,但可能会在紧要关头发挥作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多