2017-03-12 26 views
0

我有一個相當簡單的問題,那就是讓我把我的腦袋從我的桌子上撥開。我的項目使用LibGdx框架,它試圖加載一些資產。由於某種原因,它沒有在正確的文件夾中看到資產。Libgdx - 無法加載資產依賴關係

這裏是我的assetmanager類

public class LevelOneAssets { 
public static final String EARTH_TEXTURE = "earth.png"; 
public static final String MARS_TEXTURE = "mars.png"; 

private static AssetManager am; 

public static Class<Texture> TEXTURE = Texture.class; 

public static AssetManager load(){ 

    am = new AssetManager(new InternalFileHandleResolver()); 
    am.load(EARTH_TEXTURE, TEXTURE); 
    am.load(MARS_TEXTURE, TEXTURE); 
    am.update(); 
    return am; 
}} 

private void init(){ 
     Gdx.app.log("GameScreen", "Initializing"); 
     isInitialized = true; 
     am = LevelOneAssets.load(); 

     world = new World(new Vector2(0f, -9.8f), true); 
     //Add Texture Component 
     engine = new PooledEngine(); 

     RenderingSystem renderingSystem = new RenderingSystem(batch); 
     engine.addSystem(new AnimationSystem()); 
     engine.addSystem(renderingSystem); 
     engine.addSystem(new PhysicsSystem(world)); 

     engine.addSystem(new PhysicsDebugSystem(world, renderingSystem.getCamera())); 
     engine.addSystem(new UselessStateSwapSystem()); 

     am.finishLoading(); 
     Entity e = buildEarth(world); // error here 
     engine.addEntity(e); 

     e = buildMars(world); 
     engine.addEntity(e); 
     engine.addEntity(buildFloorEntity(world)); 

     isInitialized = true; 
    } 

所有資產都是LauchOff/android/assets目錄中。有任何想法嗎?我正在使用Intellij和gdxVersion = '1.9.6

編輯----

我更新的代碼,但我仍然得到Couldn't load dependencies of asset: earth.png。我覺得這是我在Idea中設置的問題,因爲我可以用gradle運行代碼。以下是我的創意設置的截圖。 Idea settings沒有太多可以出錯的地方...

回答

0

這不是使用AssetManager的正確方法。你有兩個選擇。

  1. 在遊戲線程上同步加載所有內容,等待它完成。排好所有東西后,請致電assetManager.finishLoading()在返回前完全加載所有內容。

  2. 異步加載。這允許您渲染其他內容並在紋理加載到後臺線程時繼續動畫。將所有內容排隊後,通過將更新調用放入渲染循環中,連續呼叫assetManager.update()。當一切完成加載時,此方法返回true,並且開始獲取對加載資產的引用是安全的。

偏離主題,但我建議不要對AssetManager使用靜態引用,因爲它很容易出現內存泄漏和黑色紋理。如果您這樣做,請確保您的遊戲的dispose()方法在資產經理上調用dispose()

0

爲什麼您使用ExternalFileHandleResolver,您的資源位於您的資產文件夾中,因此請使用InternalFileHandleResolver而不是ExternalFileHandleResolver。當資源是外部的時候使用ExternalFileHandleResolver(如存儲在用戶手機中的圖片)。

public static AssetManager load(){ 
    am = new AssetManager(new InternalFileHandleResolver()); 
    am.load(EARTH_TEXTURE, TEXTURE); 
    am.load(MARS_TEXTURE, TEXTURE); 
    am.finishLoading(); //Load everything synchronously otherwise make continuous call of update() method 
    return am; 
}