簡體   English   中英

LibGDX-如何使菜單屏幕切換到游戲屏幕?

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

當我運行該應用程序時,將顯示菜單屏幕,但是當我單擊屏幕以開始玩游戲時,游戲將開始播放,但菜單屏幕仍然覆蓋了游戲。 我知道這是因為游戲中有音樂播放。 將來我還將添加一個初始屏幕,但現在我不擔心。 我是新來的,所以請盡可能多地解釋。 以下是用於實現此目標的3個類。

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

}

這是主菜單屏幕

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

這是游戲畫面

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

}

除了Phonbopit的答案。 您可能應該@Override GameScreen上的渲染功能。

不確定,但我認為GameScreen上的render()方法未調用。 您必須實現使用增量時間作為參數的方法render(float delta)

更換

 public void render() { // your code } 

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM