运行新的 LibGDX 项目时“未找到文件”

我试图学习 LibGDX,我安装了所有的软件列出 给你与一个新的 Eclipse 4.3在一个新格式的 Mac OS X Maverick。

一切都很顺利,重新启动后,我下载并执行 gdx-setup.jar,填写表单,然后导入到 Eclipse 中。

当我尝试运行桌面时,没有错误,没有警告。(右键单击桌面项目 Run As-> Java Application)。

我得到了这个错误

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: badlogic.jpg
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:130)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.diesel.bugs.DieselBugs.create(DieselBugs.java:21)
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: badlogic.jpg (Local)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:134)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:218)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)

我在这里发现了很多类似的问题,我尝试了所有没有任何好的结果... 昨天晚上我发现 这个,非常酷我有最新的 Java 1.8,一个 Mac,和 Eclipse 非常适合..。

但是没有成功,我尝试用 Java 1.6和1.7,总是相同的错误(没有找到文件,我保留 Java 1.7)

我开始做一些调试,这里我唯一修改的原始代码生成的导入。

package com.diesel.bugs;


import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;


public class DieselBugs extends ApplicationAdapter {
SpriteBatch batch;
Texture imgExternal,imgLocal;


@Override
public void create () {
batch = new SpriteBatch();
String pathLocal = Gdx.files.getLocalStoragePath();
String pathExternal = Gdx.files.getExternalStoragePath();
Boolean isExternal = Gdx.files.isExternalStorageAvailable();
Boolean isLocal = Gdx.files.isLocalStorageAvailable();
imgExternal = new Texture(Gdx.files.external("/Desktop/badlogic.jpg"));
imgLocal = new Texture(Gdx.files.local("badlogic.jpg"));
}


@Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(imgExternal, 0, 0);
batch.end();
}
}

奇怪的是 pathLocal 等于“”。 Gdx.files.getLocalStoragePath ()不返回任何东西(空字符串)是正常的吗?

还有

imgExternal = new Texture(Gdx.files.external("/Desktop/badlogic.jpg"));

工作得很好。只有本地的返回错误,也是 isLocal,并且 isForeign 返回 true。

我尝试了很多组合,比如 /assets/data/badlogic.jpg/assets/badlogic.jpg/data/badlogic.jpgdata/badlogic.jpgbadlogic.jpg

图像 badlogic.jpg 在那里,我把它放在多个地方以确保。

现在我来这里寻求帮助的原因是,我只是在电脑上尝试了所有相同的步骤,一切都运行良好。

我的新电脑怎么了,还在设置?

53159 次浏览

来自 libgdx wiki

运行“桌面项目”时

第一次应用程序将失败。打开刚才创建的 Run Configuration,将工作目录设置为 android/asset/目录!

将您的桌面项目链接到 android 资产文件夹?

转到 快跑 = > 运行配置. 。 = > 选择 桌面发射器,Arguments Tab = > 工作目录 = > Other,然后浏览到 yourproject-android/asset/并单击 申请 = > 快跑

enter image description here

对于我们这些使用 Android Studio 或 IntelliJ IDEA 的人来说,你需要遵循以下步骤:

  1. 从菜单中选择 Run-> Edit Configuration

  2. 在“工作目录:”文本框中,在路径中添加“/android/asset”。

请注意,如果使用 gradle 执行任务,这不是问题。Build 文件被配置为使用来自 android 模块的 asset 文件夹。

请注意,以前的答案对于在安装时未选中“ Android”框的用户不起作用。应该为那些不关心 Android 部署的人安装一个默认资产文件夹。

对于那些使用 Intellij IDEA 的用户:

  1. 运行-> 编辑配置
  2. 在左边选择: Application-> DesktopLauncher
  3. 在“配置”选项卡中,在“工作目录”下选择你的 android/资产路径

我没有安卓项目。我的解决方案是将资产文件夹作为类文件夹添加到 JavaBuild Path-> Library。另外,检查 JavaBuildPath-> Order and Export 中的资产文件夹,以便将其包含在其他项目中。

您可以检查工作目录或链接资源中的资产路径。完成后,检查您正在加载的文件。最后清洁和刷新项目和享受。

我用了不同的方法。

Instead of creating an application run configuration, create a Gradle build configuration. The task is desktop:run. Once it is executed, the game (should) launch and stay alive without crashing, and the resources should be found.

I am not exactly sure why, but when a Gradle task is run like this, the resources are found. When an application run configuration is used (without modifications like the currently accepted answer) it crashes because it can't find the resources.

这就是你如何正确解决问题,而不是玩弄工作目录:

问题是没有正确地将资产文件夹标记为渐变构建的 resources目录。要解决这个问题,您必须在 ./core/build.gradle构建文件中添加以下代码行:

sourceSets.main.resources.srcDirs = [ "assets/" ]

My file after a clean setup with the recent libGDX version looks like this:

apply plugin: "java"


sourceCompatibility = 1.6
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'


sourceSets.main.java.srcDirs = [ "src/" ]
sourceSets.main.resources.srcDirs = [ "assets/" ]


eclipse.project {
name = appName + "-core"
}

我在 IntelliJ 遇到过同样的问题,而且我没有为安卓生成 libgdx 项目(安卓选项没有标记为勾选) ,而且我还得到了同样的错误。而不是将工作目录链接到“/android/asset/”,而是链接到“ /核心/资产”,这对我来说很好用。