3

我正在尝试显示 2 个不同的屏幕,当用户触摸屏幕时会发生变化。到目前为止,屏幕下方的代码发生了变化,但文本只是不断重叠和堆积。我需要在切换之前处理掉屏幕上的所有内容。

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

    }
}

屏幕.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);              
    }
}
4

1 回答 1

6

将您的渲染功能更改为:

@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);
}
于 2013-08-03T16:57:03.507 回答