LibGDX AssetManager没有加载任何东西

时间:2017-02-01 19:06:26

标签: java android 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();
}

}

当我启动模拟器时,它只显示黑屏,然后我的整个程序崩溃了!我做错了什么?

1 个答案:

答案 0 :(得分: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上下文。但是在初始加载屏幕期间,只要有加载条,这不重要,因此用户不会认为游戏卡住了。