AssetManager没有做任何事 - LibGDX

时间:2017-02-03 10:36:07

标签: java android libgdx asset-management

我的AssetManager类:

public class AssetLoader {

public AssetManager manager = new AssetManager();


public void load(){
    manager.load("antoanimation.png", Texture.class);
    manager.load("antoButton.png", Texture.class);
    manager.load("textures/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("textures/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.load();
    assets.manager.finishLoading();
    assets.manager.get("textures/bg2.png", Texture.class);
    assets.manager.get("logo.png", Texture.class);
    assets.manager.get("clickme.png", Texture.class);
    gsm.push(new MenuState(gsm, assets));
}



@Override
public void render() {
 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    gsm.update(Gdx.graphics.getDeltaTime());
    gsm.render(batch);



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

我的MenuState类:

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



public MenuState(GameStateManager gsm, AssetLoader assets) {
    super(gsm, assets);
    background = assets.manager.get("textures/bg2.png", Texture.class);
    play = new PlayButton();
    logo = assets.manager.get("logo.png", Texture.class);
    clickme = assets.manager.get("clickme.png", Texture.class);
    viewport = new StretchViewport(720, 1280, cam);
    viewport.apply();
    cam.position.set(cam.viewportWidth / 2, cam.viewportHeight / 2,0);

}

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

}

@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);
    sb.begin();
    sb.draw(background, 0, 0, cam.viewportWidth, cam.viewportHeight);
    sb.draw(logo, cam.viewportWidth / 2 - 310 , cam.viewportHeight / 2 + 100, 620, cam.viewportHeight / 2 - 200);
    sb.draw(clickme, 225, 125, 460, 450);
    sb.end();
    play.act();
    play.draw();

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

}

当我启动我的应用程序时,它应该在加载了所有资产后显示菜单屏幕,但它根本不会绘制任何内容!我没有收到任何错误。我该怎么办?

1 个答案:

答案 0 :(得分:-1)

你应该在你的渲染函数下调用manager.update()。 AssetManager加载资产异步。

gameloop() or render()
{
    if(mAssetManager.update())
    {
       // load screen
    }
    else{
       // game
    }
}