2013-08-03 116 views
3

我試圖顯示2個不同的屏幕,當用戶觸摸屏幕時更改。到目前爲止,隨着下面的代碼屏幕變化,但文本只是保持重疊和堆積。我需要在切換之前在屏幕上處理所有事情。LibGDX - 如何清除屏幕

其中2個類似的網頁(只有文字是在2個不同的)

package com.me.mygdxgame; 

import com.badlogic.gdx.Gdx; 
import com.badlogic.gdx.Screen; 
import com.badlogic.gdx.graphics.OrthographicCamera; 
import com.badlogic.gdx.graphics.g2d.BitmapFont; 
import com.badlogic.gdx.graphics.g2d.SpriteBatch; 
import com.badlogic.gdx.scenes.scene2d.ui.Label; 

public class MainMenu implements Screen { 
    OrthographicCamera camera; 
    SpriteBatch batch; 
    Screens game; 
    BitmapFont font; 

    public MainMenu(Screens game) { 
     this.game = game; 
    } 

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

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

    @Override 
    public void pause() { 
    } 

    @Override 
    public void resume() { 
    } 

    @Override 
    public void render(float delta) { 
     CharSequence str = "Main Menu"; 
     batch = new SpriteBatch(); 
     font = new BitmapFont(); 

     batch.begin(); 
     font.draw(batch, str, 200, 200); 
     batch.end(); 

     if (Gdx.input.justTouched()) // use your own criterion here 
      game.setScreen(game.anotherScreen); 
    } 

    @Override 
    public void show() { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void hide() { 
     // TODO Auto-generated method stub 

    } 
} 

Screens.java

package com.me.mygdxgame; 

import com.badlogic.gdx.Game; 

public class Screens extends Game { 


    MainMenu mainMenuScreen; 
    AnotherScreen anotherScreen; 


    @Override 
    public void create() { 
      mainMenuScreen = new MainMenu(this); 
      anotherScreen = new AnotherScreen(this); 
      setScreen(mainMenuScreen);    
    } 
} 

回答

5

更改渲染功能:

@Override 
public void render(float delta) { 

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  //clears the buffer 

    CharSequence str = "Main Menu"; 
    batch = new SpriteBatch(); 
    font = new BitmapFont(); 

    batch.begin(); 
    font.draw(batch, str, 200, 200); 
    batch.end(); 

    if (Gdx.input.justTouched()) // use your own criterion here 
     game.setScreen(game.anotherScreen); 
} 
+0

哦哇,我也知道。感謝您指出了這一點。我會接受答案,當它讓我 – Chris

+0

@ user2341336它爲你工作嗎? – ObieMD5

+0

@ ObieMD5是的,現在我可以爲我的應用程序製作各種頁面。接下來的任務是弄清楚如何利用後退按鈕..但那是後來 – Chris