【发布时间】:2013-08-17 01:47:45
【问题描述】:
我正在尝试将纹理加载到 libGDX,但遇到了一个找不到文件的异常。
这是尝试加载 .png 文件的代码。
//Textures
private Texture tiles;
private TextureRegion grassImage;
private TextureRegion dirtImage;
private TextureRegion stoneImage;
//Entities
private Texture entities;
private TextureRegion playerImage;
public WorldRenderer(World world, boolean debug) {
this.world = world;
this.camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
this.camera.position.set(CAMERA_WIDTH/2f, CAMERA_HEIGHT/2f, 0);
this.camera.update();
this.debug = debug;
spriteBatch = new SpriteBatch();
loadTextures();
}
public void loadTextures() {
tiles = new Texture(Gdx.files.internal("tiles.png"));
grassImage = new TextureRegion(tiles, 0, 0, 32, 32);
dirtImage = new TextureRegion(tiles, 0, 64, 32, 32);
stoneImage = new TextureRegion(tiles, 64, 0, 32, 32);
entities = new Texture(Gdx.files.internal("entities.png"));
playerImage = new TextureRegion(entities, 0, 0, 32, 32);
}
public void render() {
spriteBatch.begin();
drawGrass();
drawDirt();
drawStone();
drawPlayer();
spriteBatch.end();
if (debug) {
drawDebug();
}
}
这是错误信息:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: tiles.png
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
at com.badlogic.gdx.graphics.Texture.load(Texture.java:175)
at com.badlogic.gdx.graphics.Texture.create(Texture.java:159)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:133)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:122)
at com.mr.zen.level.WorldRenderer.loadTextures(WorldRenderer.java:62)
at com.mr.zen.level.WorldRenderer.<init>(WorldRenderer.java:58)
at com.mr.zen.screens.GameScreen.show(GameScreen.java:29)
at com.badlogic.gdx.Game.setScreen(Game.java:62)
at com.mr.zen.Zen.create(Zen.java:12)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: tiles.png (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:132)
at com.badlogic.gdx.files.FileHandle.length(FileHandle.java:586)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:220)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)
... 12 more
loadTextures() 方法中存在问题。找不到文件
tiles = new Texture(Gdx.files.internal("tiles.png"));
我确保将 .png 文件放在 android 项目文件的 assets 文件夹中。我不知道是什么原因造成的;我已经将它用于其他项目,但这次出现了问题。感谢您的帮助。
【问题讨论】:
-
在 assets 文件夹中创建一个 data 文件夹,并将 tiles.png 保存在 data 文件夹中。然后使用 tiles = new Texture(Gdx.files.internal("data/tiles.png"));我以前就是这样的
-
如果您尝试在桌面上运行它,那么您也必须将 png 放入桌面项目中。就在android上运行而言。一切看起来都很好。尝试在photoshop中导入png并再次导出然后使用它。有时这也能解决问题。
-
很多时候发生的情况是当你导出 png 由于 t photoshop 错误 libGdx 无法读取它时。这也发生在我身上。尝试从其他 PC 中保存 png。这一直发生在我身上
-
@KaptnKrunch,您使用的是 intellij 还是 eclipse?如果您使用的是intellij,则需要将桌面的工作目录设置为指向android assets文件夹。
-
如果有人像我一样遇到同样愚蠢的问题,我试图使用我下载的一些资产,它们以大写扩展名保存,因为很多事情都可能导致这个问题,所以我决定分享我的。