Can libgdx Particle Effects use texture regions?

回眸只為那壹抹淺笑 提交于 2019-12-24 08:39:09

问题


I have all of my images for a libgdx project in a single texture.

I'm about to add some nice particle effects, but the documentation implies that each type of emitter requires a separate graphics file for its particle.

Is that true? Or, is there a way of specifying a region of a texture to be used as the particle image, so that I may still keep all my images in that single file?


回答1:


Yes it can but you need to have the texture inside of an TextureAtlas. Take a look at this article for it.

here is an Example where i even use a TextureAtlas:

m_effect = new ParticleEffect();
m_effect.load(Gdx.files.internal("particle/effects/lightning.p"), this.getAtlas());

Or in 2 steps:

m_effect.loadEmitters(Gdx.files.internal("particle/effects/lightning.p"));
m_effect.loadEmitterImages(this.getAtlas());

here is whatt he LoadEmitterImage does:

public void loadEmitterImages (TextureAtlas atlas) {
    for (int i = 0, n = emitters.size; i < n; i++) {
        ParticleEmitter emitter = emitters.get(i);
        String imagePath = emitter.getImagePath();
        if (imagePath == null) continue;
        String imageName = new File(imagePath.replace('\\', '/')).getName();
        int lastDotIndex = imageName.lastIndexOf('.');
        if (lastDotIndex != -1) imageName = imageName.substring(0, lastDotIndex);
        Sprite sprite = atlas.createSprite(imageName); /// <---- here it creates a Sprite with a textureregion
        if (sprite == null) throw new IllegalArgumentException("SpriteSheet missing image: " + imageName);
        emitter.setSprite(sprite);
    }
}

src from git



来源:https://stackoverflow.com/questions/22531814/can-libgdx-particle-effects-use-texture-regions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!