我是Libgdx的新手,并且一直在关注一本书“学习LibGDX游戏开发,第2版'为了学习,现在让我使用Android Studio,问题是即使我可以在android资源文件夹中创建包文件,我得到一个GdxRuntimeException错误,表示资产未加载。错误日志如下所示:
images
cloud02 (alias of cloud01)
cloud03 (alias of cloud01)
Packing.........
Writing 1024x1024: C:\Users\Raghab\AndroidStudioProjects\CanyonBunnyTest\android\assets\images\canyonbunny.pack.png
com.packtpub.libgdx.canyonbunny.game.Assets: No of assets loader: 2
com.packtpub.libgdx.canyonbunny.game.Assets: Asset: C:/Users/Raghab/AndroidStudioProjects/CanyonBunnyTest/android/assets/images/canyonbunny.pack.png
com.packtpub.libgdx.canyonbunny.game.Assets: Asset: C:/Users/Raghab/AndroidStudioProjects/CanyonBunnyTest/android/assets/images/canyonbunny.pack.atlas
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: C:\Users\Raghab\AndroidStudioProjects\CanyonBunnyTest\android\assets\images\canyonbunny.pack.atlas
at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:110)
at com.packtpub.libgdx.canyonbunny.game.Assets.init(Assets.java:53)
at com.packtpub.libgdx.canyonbunny.CanyonBunnyMain.create(CanyonBunnyMain.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:143)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
Process finished with exit code 0
似乎即使在资产文件夹上创建了包和图像文件,我在下一行上得到一个例外,说它无法找到它,路径是绝对的,似乎不是问题,我已经粘贴了所使用的类。
桌面类:
package com.packtpub.libgdx.canyonbunny.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.tools.texturepacker.TexturePacker;
import com.packtpub.libgdx.canyonbunny.CanyonBunnyMain;
public class DesktopLauncher {
private static boolean rebuildAtlas = true;
private static boolean drawDebugOutline = true;
public static void main (String[] arg) {
if (rebuildAtlas) {
TexturePacker.Settings settings = new TexturePacker.Settings();
settings.maxWidth = 1024;
settings.maxHeight = 1024;
settings.duplicatePadding = false;
settings.debug = drawDebugOutline;
TexturePacker.process(settings, "C:\\Users\\Raghab\\AndroidStudioProjects\\CanyonBunnyTest\\desktop\\assets-raw\\images", "C:\\Users\\Raghab\\AndroidStudioProjects\\CanyonBunnyTest\\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);
}
}
资产类别:
package com.packtpub.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.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
import com.packtpub.libgdx.canyonbunny.util.Constants;
public class Assets implements Disposable, AssetErrorListener {
public static final String TAG = Assets.class.getName();
public static final Assets instance = new Assets();
private AssetManager assetManager;
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;
assetManager.setErrorListener(this);
assetManager.load(Constants.TEXTURE_ATLAS_OBJECTS, TextureAtlas.class);
assetManager.finishLoading();
Gdx.app.debug(TAG, "No of assets loader: " + assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames()) {
Gdx.app.debug((TAG), "Asset: " + a);;
}
TextureAtlas atlas = assetManager.get(Constants.TEXTURE_ATLAS_OBJECTS);
for (Texture t : atlas.getTextures()) {
t.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
}
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), "Couldn't load asset '" + asset.fileName + "'", (Exception) throwable);;
}
public class AssetBunny {
public final TextureAtlas.AtlasRegion head;
public AssetBunny(TextureAtlas atlas) {
head = atlas.findRegion("bunny_head");
}
}
public class AssetRock {
public final TextureAtlas.AtlasRegion edge;
public final TextureAtlas.AtlasRegion middle;
public AssetRock(TextureAtlas atlas) {
edge = atlas.findRegion("rock_edge");
middle = atlas.findRegion("rock_middle");
}
}
public class AssetGoldCoin {
public final TextureAtlas.AtlasRegion goldCoin;
public AssetGoldCoin(TextureAtlas atlas) {
goldCoin = atlas.findRegion("item_gold_coin");
}
}
public class AssetFeather {
public final TextureAtlas.AtlasRegion feather;
public AssetFeather(TextureAtlas atlas) {
feather = atlas.findRegion("item_feather");
}
}
public class AssetLevelDecoration {
public final TextureAtlas.AtlasRegion cloud01;
public final TextureAtlas.AtlasRegion cloud02;
public final TextureAtlas.AtlasRegion cloud03;
public final TextureAtlas.AtlasRegion mountainLeft;
public final TextureAtlas.AtlasRegion mountainRight;
public final TextureAtlas.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");
}
}
}
常数类:
package com.packtpub.libgdx.canyonbunny.util;
public class Constants {
public static final float VIEWPORT_WIDTH = 5.0f;
public static final float VIEWPORT_HEIGHT = 5.0f;
public static final String TEXTURE_ATLAS_OBJECTS =
"C:\\Users\\Raghab\\AndroidStudioProjects\\CanyonBunnyTest\\android\\assets\\images\\canyonbunny.pack.atlas";
}
答案 0 :(得分:0)
对不起所有..这个Android工作室没有正确处理路径。在定义路径时使用正斜杠而不是反斜杠。让我有一天想知道。