gpt4 book ai didi

java - LibGDX - 如何清除屏幕

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:06 24 4
gpt4 key购买 nike

我正在尝试显示 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);
}
}

最佳答案

将渲染函数更改为:

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

关于java - LibGDX - 如何清除屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18035123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com