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