简体   繁体   中英

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.

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. 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. On android it doesn't even start. 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.

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.

Try checking the option for packing required assets into the JAR. This will actually put the assets inside the JAR file and make the necessary changes to the manifest.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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