简体   繁体   English

在Java / Gdx中加载图像

[英]Loading an Image in Java/Gdx

I have been developing an android game for a while now and i have this problem. 我已经开发了一段时间的安卓游戏,我有这个问题。 Let's take this example code 让我们来看这个示例代码

class game {


   private Texture image;

   public game()
   {
       image = new Texture(Gdx.files.internal("image.png");     
   }
}

If i write my code like this everything is fine, the image loads properly and the game runs smoothly. 如果我这样编写代码,一切都很好,图像加载正确,游戏运行平稳。

The problems start when i try to create a folder inside my assets folder and load the image from there. 当我尝试在自己的Assets文件夹中创建一个文件夹并从那里加载图像时,问题就开始了。

For example 例如

class game {


   private Texture image;

   public game()
   {
       image = new Texture(Gdx.files.internal("newfolder/image.png");     
   }
}

When i do this and compile it i get no erros inside Eclipse and everything is fine. 当我这样做并编译它时,我在Eclipse中没有错误,一切都很好。 But when i try to export the project into a runnable deskop jar or as an android application, the game start for a second, shows a black screen then it stops. 但是,当我尝试将项目导出到可运行的deskop jar或作为android应用程序时,游戏开始一秒钟,显示黑屏,然后停止。 On android it doesn't even start. 在android上,它甚至无法启动。 I believe it is not able to locate and load the image and throws an exception. 我相信它无法定位和加载图像并引发异常。

Any idea why this is happening and what am I doing wrong? 知道为什么会这样,我在做什么错吗?

When you export your executable in Eclipse you need to package the required assets into the jar. 在Eclipse中导出可执行文件时,需要将所需的资产打包到jar中。

Often times when you export a JAR file it doesn't include the added library folders, and it searches for an external folder wherever the JAR is located. 通常,导出JAR文件时,它不包括添加的库文件夹,并且会搜索JAR所在位置的外部文件夹。

Try checking the option for packing required assets into the JAR. 尝试检查将所需资产打包到JAR中的选项。 This will actually put the assets inside the JAR file and make the necessary changes to the manifest. 实际上,这会将资产放入JAR文件中,并对清单进行必要的更改。

I would also double check the LibGDX wiki and make sure that your projects build paths are properly configured to use assets folder from the android project. 我还将仔细检查LibGDX Wiki,并确保您的项目构建路径已正确配置为使用android项目中的资产文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM