Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

java - LibGDX AssetManager not loading anything

I have recently added an AssetManager class to my LibGDX game, but I can't seem to get it to work.

Here's my AssetManager class:

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();
    }

This is my main game class:

    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();
        }
        }

And this is my Menu class:

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();
}

}

When I start the emulator, it just shows a black screen, and then my entire program crashes! What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are only queuing elements

the AssetManager.load() methods only adds a "loadable" resource to it, but doesn't actually load it. To do that, you need to call AssetManager.update()

Then it loads your resources, be loading one by one for each call to update().

Try this:

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 ...
}

You could then display a loading bar indicator or something while manager.update() returns false.

Note that the framerate is very low during loading, as stuff needs be loaded in the GL Thread in order to have correct access to the GL context. But during your initial loading screen, this shouldn't matter, as long as there is a loading bar, so the user doesn't think the game is stuck.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...