在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;
    }

}

最佳答案

我今天也需要实现一个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 () {
        }
    }

}


请注意,在我的项目中,我始终将图像文件与“粒子”文件放在同一文件夹中,因此这对我来说已经足够。

08-17 18:07