简体   繁体   中英

libgdx AssetManager.finishLoading() not working

I've tried to implement the AssetManager into my project. Now i can't figure out what i'm doing wrong with the AssetManager. I've googled, and tried different things. But it seems that no one else have ever experienced this kind of problem with the AssetManager. I used .finisLoading(), but afterwards if i ask .isLoaded it says that it hasn't been loaded.

The wiki tutorial here says this

manager.load("data/mytexture.png", Texture.class);

....

manager.finishLoading();

...

Texture tex = manager.get("data/mytexture.png", Texture.class);

I have this Code

// fullFile e.g. "D:\\folder\\subfolder\\subsubfolder\\jpg.jpg"

if (!new FileHandle(fullFile).exists()) {
    System.err.printf("invalid file \"%s\"\n",fullFile);
    System.exit(1);
}

if (!manager.isLoaded(fullFile,Texture.class)) manager.load(fullFile,Texture.class);
manager.finishLoading();

if (!manager.isLoaded(fullFile,Texture.class)) {
    return = new Sprite(new Texture("D:\\folder\\subfolder\\subsubfolder\\placeholder.jpg"));
} else {
    return new Sprite(manager.get(fullFile,Texture.class));
}

the manger is AssetManager manager = new AssetManager(new FileHandleResolver() { @Override public FileHandle resolve(String fullFile ) { FileHandle fh = new FileHandle(fullFile); if (!fh.exists()) { System.err.printf("invalid file \\"%s\\"\\n",fh.path()); System.exit(1); } return fh; }

What I've tried so far

Run the Project created by gdx-setup.jar -> Couldn't load file: badlogic.jpg

Code from: Juan Javier Cassani

public static void test() {
AbsoluteFileHandleResolver fileHandleResolver = new AbsoluteFileHandleResolver();
AssetManager manager = new AssetManager(fileHandleResolver);
//This could be anywhere else
String fileName = "D:\\blablabla\\my_project\\core\\assets\\badlogic.jpg";

if (!new FileHandle(fileName).exists()) {
    Gdx.app.error("Blub", "invalid file '" + fileName + "'");
    Gdx.app.exit();
}

if (!manager.isLoaded(fileName, Texture.class))
    manager.load(fileName, Texture.class);

manager.finishLoading();

if (manager.isLoaded(fileName, Texture.class))
    Gdx.app.log("Blub", "Texture loaded!");
else
    Gdx.app.log("Blub", "Texture not loaded!");
}

-> Blub: Texture not loaded!

Versions: 
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

ashley-1.3.1.jar
box2dlights-1.3.jar
gdx-1.4.1.jar
gdx-ai-1.4.0.jar
gdx-backend-lwjgl-1.4.1.jar
gdx-box2d-1.4.1.jar
gdx-box2d-platform-1.4.1-natives-desktop.jar
gdx-bullet-1.4.1.jar
gdx-bullet-platform-1.4.1-natives-desktop.jar
gdx-controllers-1.4.1.jar
gdx-controllers-desktop-1.4.1.jar
gdx-controllers-platform-1.4.1-natives-desktop.jar
gdx-freetype-1.4.1.jar
gdx-freetype-platform-1.4.1-natives-desktop.jar
gdx-platform-1.4.1-natives-desktop.jar
gdx-tools-1.4.1.jar
jinput-2.0.5.jar
jinput-platform-2.0.5-natives-linux.jar
jinput-platform-2.0.5-natives-osx.jar
jinput-platform-2.0.5-natives-windows.jar
jlayer-1.0.1-gdx.jar
jorbis-0.0.17.jar
jutils-1.0.0.jar
lwjgl-2.9.1.jar
lwjgl-platform-2.9.1-natives-linux.jar
lwjgl-platform-2.9.1-natives-osx.jar
lwjgl-platform-2.9.1-natives-windows.jar
lwjgl_util-2.9.1.jar

I assume you are using absolute (full) file paths (because in the provided code its just commented), but AssetManager expects internals (you are creating it using default constructor, not specifying a FileHandleResolver). I tried your code using

String fileName = "texture.jpg";

and putting a texture.jpg file in /project/android/assets/ and it works fine. Just put your internal assets in that folder, change your file path to internal and it should work.

Edit:

I really think that using absolute paths is not a good idea, since not all backends accept it, and even if you only deploy to desktop, it makes non sense to have your assets in a location outside your jar is. Anyway, this code works for me, for absolute paths:

      AbsoluteFileHandleResolver fileHandleResolver = new AbsoluteFileHandleResolver();
      AssetManager manager = new AssetManager(fileHandleResolver);
      //This could be anywhere else
      String fileName = "/home/javier/texture.jpg";

      if (!new FileHandle(fileName).exists()) {
            Gdx.app.error(TAG, "invalid file '" + fileName + "'");
            Gdx.app.exit();
      }

      if (!manager.isLoaded(fileName, Texture.class))
            manager.load(fileName, Texture.class);

      manager.finishLoading();

      if (manager.isLoaded(fileName, Texture.class))
            Gdx.app.log(TAG, "Texture loaded!");
      else
            Gdx.app.log(TAG, "Texture not loaded!");

I just read (again) here that i had to set the working directory to your_project_path/core/assets/ . I somehow forgot that step after setup.

But the asset folder needs to flat, no sub directories (on desktop).

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