Java libgdx加载资源,纹理打包器
很抱歉打扰您,我知道已经有人问过了,但我无法解决它。我已经开始与libgdx合作,并决定将一本书作为教程——《学习LigbGDX游戏开发》。在第四章之前一切都很顺利,我无法将资产加载到canyonbunny.pack。我不知道发生了什么事。在../CanyonBunny android/assets/images中创建了两个文件-一个是png,另一个是atlas。如果你能帮我一把,我将非常欢迎Java libgdx加载资源,纹理打包器,java,libgdx,texturepacker,Java,Libgdx,Texturepacker,很抱歉打扰您,我知道已经有人问过了,但我无法解决它。我已经开始与libgdx合作,并决定将一本书作为教程——《学习LigbGDX游戏开发》。在第四章之前一切都很顺利,我无法将资产加载到canyonbunny.pack。我不知道发生了什么事。在../CanyonBunny android/assets/images中创建了两个文件-一个是png,另一个是atlas。如果你能帮我一把,我将非常欢迎 DesktopLauncher.java package com.packtub.libgdx.ca
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\assets\images\CanyonBunny.pack.png
com.packtub.libgdx.canyonbunny.game.Assets:无se pudo cargar el资产
'images/canyonbuny.pack,com.badlogic.gdx.graphics.g2d.TextureAtlas'
com.badlogic.gdx.utils.GdxRuntimeException:未找到文件:
images\canyonbuny.pack(内部)位于
com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)位于
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)
在
AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:98)
在
AssetLoadingTask.update(AssetLoadingTask.java:87)
在
com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:466)
位于com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:354)
在
AssetManager.finishLoading(AssetManager.java:377)
位于com.packtub.libgdx.canyonbuny.game.Assets.init(Assets.java:35)
在
com.packtub.libgdx.canyonbuny.canyonbunymain.create(canyonbunymain.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:未加载资产:
图片/canyonbunny.pack位于
com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:110)位于
com.packtub.libgdx.canyonbuny.game.Assets.init(Assets.java:40)位于
com.packtub.libgdx.canyonbuny.canyonbunymain.create(canyonbunymain.java:25)
在
com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137)
在
com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
TexturePacker自动放置settings.atlasExtension(“.atlas”)定义的扩展名
更改此设置或将Constants.TEXTURE_ATLAS_OBJECTS变量的值更改为“canyonbunny.pack.ATLAS”。将设置atlasExtension设置为“”
if (rebuildAtlas) {
Settings settings = new Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
settings.duplicatePadding = false;
settings.atlasExtension="";
...
}