使用LibGDX Gdx.files.internal加载.png文件时遇到问题

Kap*_*nch 7 java libgdx

我正在尝试将纹理加载到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();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是错误消息:

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
Run Code Online (Sandbox Code Playgroud)

loadTextures()方法中存在问题.找不到该文件

 tiles = new Texture(Gdx.files.internal("tiles.png"));
Run Code Online (Sandbox Code Playgroud)

我已经确保将.png文件放在android项目文件的assets文件夹中.我不知道造成这种情况的原因; 我已经把这个用于其他项目,但这次出现了问题.谢谢你的帮助.

yur*_*ric 10

通过清理项目解决"无法加载文件"问题(Eclipse)

(对于此解决方案,预计图像文件的路径是正确的.)

有时我在向项目添加新图像时无法加载文件问题.我不知道为什么,但我的桌面版本的LibGDX游戏没有用我的新图像(添加到Mygame-android/assets/data)更新文件夹Mygame-desktop/bin/data.(也就是说,每次保存文件时,Eclipse都会构建项目,因为我设置了Project> Build Automatically选项.)

它应该工作,因为在LibGDX中,桌面和HTML项目都有一个指向Android项目资产文件夹的链接.所以,我不认为需要手动将我的新图像复制到Mygame-desktop/bin/data.

该解决方案在Eclipse上,清理(菜单项目>清洁...),你用你的游戏中所有LibGDX项目,然后在文件Mygame-的Android /资产/数据将被复制到Mygame桌面/斌/数据正确.


小智 6

如果您正在使用Gradle,请单击包浏览器中的鼠标左键 Aassets -> Gradle -> Refresh All.

就是这样=)