简体   繁体   中英

LibGDX - how do I make my menu screen switch to the Game screen?

When I run the application my menu screen shows, but when I click the screen to begin playing the game the game begins playing but the menu screen is still their overlaying the game. I know this because the game has music playing. I'm also going to add a splash screen in the future but I'm not concerned about that right now. I'm new at this so please explain things as best as you can. Below are the 3 classes used to make this happen.

public class SlingshotSteve extends Game {

public SpriteBatch batch;
public BitmapFont font;

public void create() {
batch = new SpriteBatch();
//Use LibGDX's default Arial font.
font = new BitmapFont();
this.setScreen(new Menu(this));
}

public void render() {
super.render(); //important!
}

public void dispose() {
batch.dispose();
font.dispose();
}

}

Here is the main menu screen

public class Menu implements Screen {

final SlingshotSteve game;

OrthographicCamera camera;

public Menu(final SlingshotSteve gam) {
game = gam;

camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
}

@Override
public void render(float delta) {

Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

camera.update();
game.batch.setProjectionMatrix(camera.combined);

game.batch.begin();
game.font.draw(game.batch, "Welcome to Slingshot Steve!!! ", 100, 150);
game.font.draw(game.batch, "Tap anywhere to begin!", 100, 100);
game.batch.end();

if (Gdx.input.isTouched()) {
    game.setScreen((Screen) new GameScreen(game));
    dispose();
}
}

Here is the game screen

public class GameScreen implements Screen {
final SlingshotSteve game;   

OrthographicCamera camera;
// Creates our 2D images
SpriteBatch batch;
TextureRegion backgroundTexture;
Texture texture;

GameScreen(final SlingshotSteve gam) {
    this.game = gam; 

camera = new OrthographicCamera(1280, 720);

batch = new SpriteBatch();

Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);

Music mp3Sound = Gdx.audio.newMusic(Gdx.files.internal("rain.mp3"));
mp3Sound.setLooping(true);
mp3Sound.play();



}


public void render() {  

  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

  camera.update();
  batch.setProjectionMatrix(camera.combined);

  batch.begin();
  batch.draw(backgroundTexture, 0, 0); 
  batch.end();

}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void dispose() {
   batch.dispose();
   texture.dispose();

}

In addition to Phonbopit 's answer. You should probably @Override the render function at GameScreen.

Not sure but I think your render() method on GameScreen not called. you must implement method render(float delta) that use delta time for parameter.

replace

 public void render() { // your code } 

with

public void render(float delta) {
    // your code
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM