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

时间:2023-03-01
本文介绍了使用 LibGDX Gdx.files.internal 加载 .png 文件时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试将纹理加载到 libGDX,但遇到了一个找不到文件的异常.

I'm trying to load a Texture to libGDX and I'm getting a file not found exception.

这是尝试加载 .png 文件的代码.

Here's the code that's trying to load the .png file.

//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() 方法中存在问题.找不到文件

The problem exists in the loadTextures() method. Can't find the file

 tiles = new Texture(Gdx.files.internal("tiles.png"));

我确保将 .png 文件放在 android 项目文件的 assets 文件夹中.我不知道是什么原因造成的;我已经将它用于其他项目,但这次出现了问题.感谢您的帮助.

I've made sure to put the .png files in the assets folder of the android project files. I have no idea what's causing this; I've gotten this to work on other projects but this time around something went awry. Thanks for any help.

推荐答案

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

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

Solving the "Couldn't load file" issue by cleaning the project (Eclipse)

(For this solution is expected that the paths to your image files are correct.)

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

Sometimes I have this Couldn't load file problem when I add new images to my project. I don't know why, but my Desktop version of my LibGDX game doesn't update the folder Mygame-desktop/bin/data with my new images (which were added to Mygame-android/assets/data) when the project is built. (That is, every time I save a file, Eclipse builds the project, cause I set the option Project > Build Automatically.)

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

It should work, cause in LibGDX the Desktop and HTML projects have a link to the Android project assets folder. So, I was not suppose to need to copy manually my new images to Mygame-desktop/bin/data.

在 Eclipse 上,解决方案是清理(菜单 Project > Clean...)您用于游戏的所有 LibGDX 项目,然后是 Mygame-android/assets/中的文件data 将被正确复制到 Mygame-desktop/bin/data.

The solution, on Eclipse, is to clean (menu Project > Clean...) all LibGDX projects you use for your game, then the files in Mygame-android/assets/data will be copied to Mygame-desktop/bin/data properly.

这篇关于使用 LibGDX Gdx.files.internal 加载 .png 文件时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:覆盖绘图顺序scene2D的舞台 下一篇:拖动窗口时未调用 Libgdx 暂停/恢复 (Windows)

相关文章