我正在使用 libgdx 学习和编写 Android 游戏,并且我已经在这个错误上停留了很长一段时间了。我附上以下内容
- 代码。
- 我用来访问 Assets 的命令。
- 包含我在 Android 上调试代码时出现的错误的屏幕截图。
- 我的 Package Explorer 的屏幕截图。
- 我尝试过让代码正常运行的所有可能的组合。
1.代码
package com.me.mygdxgame;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetErrorListener;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.utils.Disposable;
public class Gameassetloader implements Disposable, AssetErrorListener {
public static final String TAG = Gameassetloader.class.getName();
public static Gameassetloader instance = new Gameassetloader();
private AssetManager assetManager;
/** Load the appropriate texture for the respective entities **/
public AssetCharacter boy;
public AssetRock rock;
public AssetShield shield;
/** singleton: prevent instantiation from other classes **/
private Gameassetloader(){}
public void init(AssetManager assetManager)
{
this.assetManager = assetManager;
//set asset manager error handler
assetManager.setErrorListener(this);
//load texture atlas
assetManager.load(Gameconstants.TEXTURE_ATLAS_OBJECTS,TextureAtlas.class);
//start loading assets and wait until finished
assetManager.finishLoading();
Gdx.app.debug(TAG, "# of assets loaded:" + assetManager.getAssetNames().size);
for (String a : assetManager.getAssetNames())
Gdx.app.debug(TAG, "asset:" + a);
TextureAtlas atlas = assetManager.get(Gameconstants.TEXTURE_ATLAS_OBJECTS);
//enable texture filtering for pixel smoothing
for(Texture t : atlas.getTextures())
t.setFilter(TextureFilter.Linear,TextureFilter.Linear);
//create game resource objects
boy = new AssetCharacter(atlas);
rock = new AssetRock(atlas);
shield = new AssetShield(atlas);
}
public void dispose() { assetManager.dispose(); }
@Override
public void error(String fileName, Class type, Throwable throwable)
{
Gdx.app.error(TAG, "Couldn't load asset' " + fileName + "'", (Exception)throwable);
}
}
2.命令
public static final String TEXTURE_ATLAS_OBJECTS ="gdxgame-android/assets/packed/packed.png";
3.Package Explorer截图 http://i1294.photobucket.com/albums/b608/Abhishek_M369/PackageExplorer_zpsd2589ee2.jpg
4.错误日志截图(Android DDMS) http://i1294.photobucket.com/albums/b608/Abhishek_M369/ErrorLog_zps8a4a68d9.jpg
5.我尝试过的所有可能的组合
"/gdxgame-android/assets/packed/packed.png";
"gdxgame-android/assets/packed/packed.png";
"/assets/packed/packed.png";
"assets/packed/packed.png";
"/packed/packed.png";
"packed/packed.png";
"/packed.png";
"packed.png";
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.toString();
//this caused shutdown of emulator
public static FileHandle location = Gdx.files.internal("assets/packed.png");
public static final TEXTURE_ATLAS_OBJECT = location.readString();
// I even tried setting the build path of the asset folder as source folder
// Also tried placing the image in the data folder.
// Tried using the absolute path too , i.e Gdx.files.absolute("the absolute");
// Tried passing the absolute path directly as string
似乎没有什么对我有用。
最佳答案
问题很简单,问题出在libgdx的版本和文档不同。下面解释的答案将仅确认 libgdx 版本 0.9.8。
(注意:使用Texturepacker GUI来打包纹理&&而不是libgdx库中的方法)
首先,必须向“assetManager”提供一个包含图像坐标的文件,这是一个错误,因为我提供了打包图像。
GL20 应该能够解析 NPOT 图像,但它无法这样做,原因仍然未知,所以我不得不将纹理打包到 POT 中,这是通过在 GUI 中选择 POT 选项来完成。完成此操作后,我可以使用以下代码轻松加载新的 POT 图像
/**Mention only the folder/file under the asset dir**/
public class Gameconstants { public static final String location = "packed/packed.txt" }
/**access the same using the following command**/
private AssetManager assetManager;
assetManager.load(Gameconstants.location,Texture.class);
这个答案可能不太有说服力,但它确实解决了我的问题。
感谢所有提供帮助的人:)
关于java - Gdx运行时异常: Error reading file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25012478/