libgdx加载资产,纹理包装

时间:2015-01-27 18:45:52

标签: java libgdx texturepacker

抱歉打扰你,我知道这已经被问到了,但是我无法解决这个问题。我已经开始使用libgdx并决定有一本书作为教程 - “学习LigbGDX游戏开发”。直到第4章一切顺利,我无法将资源加载到canyonbunny.pack。我不知道发生了什么。在../CanyonBunny-android/assets/images中创建了两个文件 - 一个是png,另一个是atlas。如果你能帮助我,我会受到欢迎。

DesktopLauncher.java

package com.packtub.libgdx.canyonbunny.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.packtub.libgdx.canyonbunny.CanyonBunnyMain;
import com.badlogic.gdx.tools.texturepacker.TexturePacker;
import com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings;


public class DesktopLauncher {
    private static boolean rebuildAtlas = true;
    private static boolean drawDebugOutline = true;

    public static void main (String[] args) {
        if (rebuildAtlas){
            Settings settings = new Settings();
            settings.maxWidth = 1024;
            settings.maxHeight = 1024;
            settings.debug = drawDebugOutline;
            TexturePacker.process(settings, "assets-raw/images", 
                    "..CanyonBunny-android/assets/images",
                    "canyonbunny.pack");
        }

        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();

        config.title = "CanyonBunny";
        config.useGL30 = false;
        config.width = 800;
        config.height = 480;
        new LwjglApplication(new CanyonBunnyMain(), config);
    }
}

Assets.java

package com.packtub.libgdx.canyonbunny.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
import com.packtub.libgdx.canyonbunny.util.Constants;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;

public class Assets implements Disposable, AssetErrorListener{
    public static final String TAG = Assets.class.getName();
    public static final Assets instance = new Assets();
    private AssetManager assetManager;

    //singleton: previene de inicializaciones desde otras clases
    private Assets () {}

    public AssetBunny bunny;
    public AssetRock rock;
    public AssetGoldCoin goldCoin;
    public AssetFeather feather;
    public AssetLevelDecoration levelDecoration;

    public void init (AssetManager assetManager){
        this.assetManager = assetManager;
        //Se establece el asset manager que maneja los errores
        assetManager.setErrorListener(this);
        //Carga las texturas atlas
        assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
        //Se inicia a cargar los assets y se espera que termine
        assetManager.finishLoading();
        Gdx.app.debug(TAG, "# de assets cargados: " 
        + assetManager.getAssetNames().size);
        for (String a : assetManager.getAssetNames()) Gdx.app.debug(TAG, "asset: " + a);

        TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);

        //activa la opcion "texture filtering" para una dibujado mas suave de los pixeles
        for (Texture t : atlas.getTextures()) t.setFilter(TextureFilter.Linear, TextureFilter.Linear);

        //Crea los objetos del juego
        bunny = new AssetBunny(atlas);
        rock = new AssetRock(atlas);
        goldCoin = new AssetGoldCoin(atlas);
        feather = new AssetFeather(atlas);
        levelDecoration = new AssetLevelDecoration(atlas);
    }

    @Override 
    public void dispose(){
        assetManager.dispose();
    }

    @Override
    public void error(AssetDescriptor asset, Throwable throwable) {
        Gdx.app.error(TAG, "No se pudo cargar el asset '" + asset + "'", (Exception)throwable);

    }

    //public void error(String filename, Class type, Throwable throwable){
        //Gdx.app.error(TAG, "No se pudo cargar el asset '" + filename + "'", (Exception)throwable);

    //}

    public class AssetBunny {
        public final AtlasRegion head;
        public AssetBunny (TextureAtlas atlas){
            head = atlas.findRegion("bunny_head");
        }
    }

    public class AssetRock {
        public final AtlasRegion edge;
        public final AtlasRegion middle;
        public AssetRock(TextureAtlas atlas){
            edge = atlas.findRegion("rock_edge");
            middle = atlas.findRegion("rock_middle");
        }
    }

    public class AssetGoldCoin {
        public final AtlasRegion goldCoin;
        public AssetGoldCoin(TextureAtlas atlas){
            goldCoin = atlas.findRegion("item_gold_coin");
        }
    }

    public class AssetFeather{
        public final AtlasRegion feather;
        public AssetFeather(TextureAtlas atlas){
            feather = atlas.findRegion("item_feather");
        }
    }

    public class AssetLevelDecoration{
        public final AtlasRegion cloud01;
        public final AtlasRegion cloud02;
        public final AtlasRegion cloud03;
        public final AtlasRegion mountainLeft;
        public final AtlasRegion mountainRight;
        public final AtlasRegion waterOverlay;

        public AssetLevelDecoration(TextureAtlas atlas){
            cloud01 = atlas.findRegion("cloud01");
            cloud02 = atlas.findRegion("cloud02");
            cloud03 = atlas.findRegion("cloud03");
            mountainLeft = atlas.findRegion("mountain_left");
            mountainRight = atlas.findRegion("mountain_right");
            waterOverlay = atlas.findRegion("water_overlay");
        }
    }




}
  

控制台

     

图像包装.........写入1024x1024:   ..CanyonBunny-的Android \资产\影像\ canyonbunny.pack.png   com.packtub.libgdx.canyonbunny.game.Assets:没有se pudo cargar el资产   'images / canyonbunny.pack,com.badlogic.gdx.graphics.g2d.TextureAtlas'   com.badlogic.gdx.utils.GdxRuntimeException:找不到文件:   images \ canyonbunny.pack(内部)at   com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)at   com.badlogic.gdx.graphics.g2d.TextureAtlas $ TextureAtlasData。(TextureAtlas.java:103)     在   com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:58)     在   com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34)     在   com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:98)     在   com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:87)     在   com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:466)     在com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:354)     在   com.badlogic.gdx.assets.AssetManager.finishLoading(AssetManager.java:377)     在com.packtub.libgdx.canyonbunny.game.Assets.init(Assets.java:35)     在   com.packtub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:25)     在   com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)     在   com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:114)   com.packtub.libgdx.canyonbunny.game.Assets:#de assets cargados:0   线程“LWJGL应用程序”中的异常   com.badlogic.gdx.utils.GdxRuntimeException:未加载资产:   images / canyonbunny.pack at   com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:110)at   com.packtub.libgdx.canyonbunny.game.Assets.init(Assets.java:40)at at   com.packtub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:25)     在   com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)     在   com.badlogic.gdx.backends.lwjgl.LwjglApplication $ 1.run(LwjglApplication.java:114)

2 个答案:

答案 0 :(得分:2)

TexturePacker自动放置由settings.atlasExtension(“.atlas”)定义的扩展名。

更改此设置或将Constants.TEXTURE_ATLAS_OBJECTS变量的值更改为“canyonbunny.pack.atlas”。

答案 1 :(得分:0)

将atlasExtension设置为“”。

if (rebuildAtlas) {
            Settings settings = new Settings();
            settings.maxWidth = 1024;
            settings.maxHeight = 1024;
            settings.duplicatePadding = false;
            settings.atlasExtension="";
...
}