1

在libgdx中,AssetManager没有ParticleEffectLoader。所以我为粒子效果编写了一个加载器。但是总是出现错误并说无法加载文件。

任何人都可以帮我解决问题或向我展示一个成功的粒子效果加载器吗?非常感谢!

我的代码:

package com.game.data;

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.AsynchronousAssetLoader;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.utils.Array;

public class ParticleEffectLoader extends 
    AsynchronousAssetLoader<ParticleEffect, AssetLoaderParameters<ParticleEffect>>{

    ParticleEffect particleEffect;

    public ParticleEffectLoader (FileHandleResolver resolver) {
        super(resolver);
        if(particleEffect==null){
            particleEffect=new ParticleEffect();
        }
    }

    @Override
    public void loadAsync(AssetManager manager, String fileName,
            AssetLoaderParameters<ParticleEffect> parameter) {
        System.out.println("loadAsync");
        FileHandle particleFile = resolve(fileName);
        particleEffect.loadEmitters(particleFile);
        particleEffect.loadEmitterImages(imagesDir);
    }

    @Override
    public ParticleEffect loadSync(AssetManager manager, String fileName,
            AssetLoaderParameters<ParticleEffect> parameter) {
        System.out.println("loadSync");
        return particleEffect;
    }

    @Override
    public Array<AssetDescriptor> getDependencies(String fileName,
            AssetLoaderParameters<ParticleEffect> parameter) {
        System.out.println("getDependencies");
//      FileHandle imagesDir = resolve("res/");
//      particleEffect.loadEmitterImages(imagesDir);
        return null;
    }

}
4

2 回答 2

2

我今天也需要实现一个 ParticleEffectLoader,很高兴发现它非常简单。下面是我的实现,我在自己的项目中测试过,我使用SynchronousAssetLoader.

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetLoaderParameters;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.SynchronousAssetLoader;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.utils.Array;

/**
 * @author Daniel
 * @date Oct 10, 2012 2:04:42 PM
 */
public class ParticleEffectLoader extends SynchronousAssetLoader<ParticleEffect, ParticleEffectLoader.ParticleEffectParameter> {

    public ParticleEffectLoader(FileHandleResolver resolver) {
        super(resolver);
    }

    @Override
    public ParticleEffect load(AssetManager assetManager, String fileName,
            ParticleEffectParameter parameter) {
            ParticleEffect effect = new ParticleEffect();
        FileHandle effectFile = resolve(fileName);
        FileHandle imgDir = effectFile.parent();
        effect.load(effectFile, imgDir);
        return effect;
    }

    @Override
    public Array<AssetDescriptor> getDependencies(String fileName,
            ParticleEffectParameter parameter) {
        return null;
    }

    static public class ParticleEffectParameter extends AssetLoaderParameters<ParticleEffect> {
        public ParticleEffectParameter () {
        }
    }

}

请注意,在我的项目中,我总是将图像文件与粒子文件放在同一个文件夹中,所以这对我来说已经足够了。

于 2012-10-10T07:01:10.567 回答
2

只是为了完整起见:同时有一个官方的 ParticleEffectsLoader: http: //libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/assets/loaders/ParticleEffectLoader.html

于 2016-12-22T20:52:33.450 回答