2017-02-01 203 views
-1

我最近添加了一個AssetManager類到我的LibGDX遊戲,但我似乎無法讓它工作。LibGDX AssetManager沒有加載任何東西

這裏是我的AssetManager類:

public class AssetLoader { 

    public AssetManager manager = new AssetManager(); 

    public void loadAssets(){ 
     manager.load("antoanimation.png", Texture.class); 
     manager.load("antoButton.png", Texture.class); 
     manager.load("bg2.png", Texture.class); 
     manager.load("bottomtube2.png", Texture.class); 
     manager.load("clickme.png", Texture.class); 
     manager.load("diamtermopan.png", Texture.class); 
     manager.load("gameover.png", Texture.class); 
     manager.load("grass.png", Texture.class); 
     manager.load("logo.png", Texture.class); 
     manager.load("menubutton.png", Texture.class); 
     manager.load("pausebutton.png", Texture.class); 
     manager.load("pausemenu.png", Texture.class); 
     manager.load("play.png", Texture.class); 
     manager.load("restartbutton.png", Texture.class); 
     manager.load("return.png", Texture.class); 
     manager.load("selected.png", Texture.class); 
     manager.load("shopbutton.png", Texture.class); 
     manager.load("shopLayout.png", Texture.class); 
     manager.load("termopanButton.png", Texture.class); 
     manager.load("vladanimation.png", Texture.class); 
     manager.load("vladButton.png", Texture.class); 
    } 



    public void dispose(){ 
     manager.dispose(); 
    } 

這是我主要的遊戲類:

public class FlappyPela extends ApplicationAdapter { 
     public static SpriteBatch batch; 
     private GameStateManager gsm; 
     private AssetLoader assets; 


     @Override 
     public void create() { 
      batch = new SpriteBatch(); 
      gsm = new GameStateManager(); 
      assets = new AssetLoader(); 
      assets.loadAssets(); 
      assets.manager.finishLoading(); 
      gsm.push(new MenuState(gsm)); 
     } 



     @Override 
     public void render() { 
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
      super.render(); 




     } 
     @Override 
     public void dispose() { 
      super.dispose(); 
      assets.manager.dispose(); 
      batch.dispose(); 
     } 
     } 

這是我的菜單類:

public class MenuState extends State { 
    private Texture background; 
    private Sprite logo; 
    private Sprite clickme; 
    PlayButton play; 
    Viewport viewport; 



public MenuState(GameStateManager gsm) { 
    super(gsm); 
    background = new Texture("bg2.png"); 
    play = new PlayButton(); 
    logo = new Sprite(new Texture(Gdx.files.internal("logo.png"))); 
    clickme = new Sprite(new Texture(Gdx.files.internal("clickme.png"))); 
    viewport = new StretchViewport(720, 1280, cam); 
    viewport.apply(); 
    cam.position.set(cam.viewportWidth/2, cam.viewportHeight/2,0); 
    logo.setSize(620, cam.viewportHeight/2 - 200); 
    logo.setPosition(cam.viewportWidth/2 - logo.getWidth()/2 , cam.viewportHeight/2 + 100); 
    clickme.setSize(460, 450); 
    clickme.setPosition(225, 125); 

} 

@Override 
public void handleInput() { 
    if(play.isPressed()){ 
     gsm.set(new PlayState(gsm)); 
    } 

} 

@Override 
public void update(float dt) { 
    handleInput(); 
    cam.update(); 

} 
@Override 
public void resize(int width, int height){ 
    viewport.update(width, height); 
    cam.position.set(cam.viewportWidth/2, cam.viewportHeight/2,0); 
} 


@Override 
public void render(SpriteBatch sb) { 
    cam.update(); 
    sb.setProjectionMatrix(cam.combined); 
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
    sb.begin(); 
    sb.draw(background, 0, 0, cam.viewportWidth, cam.viewportHeight); 
    logo.draw(sb); 
    clickme.draw(sb); 
    sb.end(); 
    play.act(); 
    play.draw(); 

} 
@Override 
public void dispose() { 
    background.dispose(); 
    logo.getTexture().dispose(); 
    clickme.getTexture().dispose(); 
} 

}

當我開始模擬器,它只是顯示黑屏,然後我的整個程序崩潰!我究竟做錯了什麼?

+0

顯示你的崩潰報告? – Aryan

+0

您沒有使用/從AssetManger獲取您創建的數據。 – Aryan

+0

另外我需要你的狀態類的代碼? – Aryan

回答

0

你只排隊元素

AssetManager.load()方法只是增加了一個「加載」資源,但實際上並沒有加載它。要做到這一點,你需要撥打AssetManager.update()

然後它會加載您的資源,每次調用update()時都要逐個加載。

試試這個:

public void render() { 
     if(manager.update()) { 
     // we are done loading, let's move to another screen! 
     } 

     // display loading information 
     float progress = manager.getProgress() 
     ... left to the renderer, display loading bar or somthing ... 
} 

然後,您可以顯示加載條指示器或東西,而manager.update()返回false。

請注意,加載過程中的幀率非常低,因爲需要將東西加載到GL線程中才能正確訪問GL上下文。但是,在初始加載屏幕時,只要有加載欄,這應該沒有關係,所以用戶不認爲遊戲卡住了。