libgdx如何创建自己的textureregionLoader

时间:2018-08-13 10:35:39

标签: java android libgdx

        Texture ts=am.get("gametiles.o", MyTexture.class);

    tiles_1 = new TextureRegion(ts, 0, 2, 116, 32);

    tiles_2 = new TextureRegion(ts, 0, 36, 116, 32);
    tiles_3 = new TextureRegion(ts, 0, 72, 116, 32);
    tiles_4 = new TextureRegion(ts, 0, 109, 116, 32);
    tiles_5 = new TextureRegion(ts, 1026, 258, 116, 32);

我认为这还不够整洁,并且不使用资产管理器,所以我决定创建我的textureregionLoder

public class MyTextureRegionLoader extends  AsynchronousAssetLoader<TextureRegion,MyTextureRegionLoader.MyTextureRegionParameter>

{

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle p2, MyTextureRegionLoader.MyTextureRegionParameter parameter)
{

    if (parameter != null)
    {
        return null;
    }
    Array<AssetDescriptor>array=new Array<AssetDescriptor>();
    array.add(new AssetDescriptor< MyTexture>(fileName, MyTexture.class));
    return array;

}

@Override
public void loadAsync(AssetManager p1, String p2, FileHandle p3, MyTextureRegionLoader.MyTextureRegionParameter parameter)
{/*if(parameter==null){
     throw new GdxRuntimeException("plase add data");
     }*/
}

@Override
public TextureRegion loadSync(AssetManager manger, String filename, FileHandle p3, MyTextureRegionLoader.MyTextureRegionParameter parameter)
{  
    if (parameter == null)
    {
        MyTexture mT=manger.get(filename, MyTexture.class);
        TextureRegion tr=new TextureRegion(mT);
        return tr;}
    else
    {if(manger.isLoaded(parameter.TextureFileName,MyTexture.class)){
        Gdx.app.error("","加载完毕");
        MyTexture mt=manger.get(parameter.TextureFileName, MyTexture.class);

        Gdx.app.error("","texture地址"+mt.toString());
        TextureRegion tr=new TextureRegion(mt, parameter.x, parameter.y, parameter.width, parameter.height);
        return tr;}
        else{
            return null;
        }
    }
}
public MyTextureRegionLoader(FileHandleResolver fh)
{
    super(fh);
}
public static class MyTextureRegionParameter extends AssetLoaderParameters<TextureRegion>
{
    public String TextureFileName;
    public float x;
    public float y;
    public float width;
    public float height;}

}

  

因为我想采用AssetManager和Recycler的纹理

    am.load("x.o", TextureRegion.class);
  

成功了

am.load("texture.o", MyTexture.class);
    MyTextureRegionLoader.MyTextureRegionParameter n=new MyTextureRegionLoader.MyTextureRegionParameter();
    n.TextureFileName = "texture.o";
    n.x = 0;
    n.y = 2;
    n.width = 116;
    n.height = 32;
    am.load("kk", TextureRegion.class, n);`
    tiles_1=am.get("kk",TextureRegion.class);`
  

当我使用图块时,它没有出现在屏幕上,并且应用程序没有快速退出,日志正常,如何解决此问题?

0 个答案:

没有答案