libgdx: 切换屏幕时图像不会消失
libgdx: Images don't disappear when I switch screen
此时我有 2 个屏幕:MainScreen
(这是游戏的主菜单)和 GameScreen
。我有一个 "button"(它是一个图像),当用户点击它时,MainScreen
切换到 GameScreen
。
问题是当第二个屏幕(按钮和主标题)出现时,MainScreen
的所有图像都没有消失。我已经为此工作了好几天了,我不知道还能做什么。
主要class游戏:
public class MyGame extends Game {
@Override
public void create () {
Sounds.load();
Texts.load();
Buttons.load();
setScreen(new MainScreen(this));
}
@Override
public void dispose () {
super.dispose();
Sounds.dispose();
Texts.dispose();
Buttons.dispose();
}
}
主屏幕:
public class MainScreen implements Screen {
private MyGame game;
private Stage stage;
private OrthographicCamera camera;
private Viewport viewport;
public static Image tituloPrincipal, botonPlay;
public MainScreen(final MyGame game) {
this.game = game;
//stage
camera = new OrthographicCamera(Settings.SCREEN_WIDTH, Settings.SCREEN_HEIGHT);
viewport = new StretchViewport(Settings.SCREEN_WIDTH, Settings.SCREEN_HEIGHT, camera);
stage = new Stage(viewport);
//background
stage.addActor(new Image(Sprites.mainBackground));
//Title of the game
tituloPrincipal = new Image();
tituloPrincipal.setPosition(Settings.SCREEN_WIDTH * 1 / 6, Settings.SCREEN_HEIGHT * 4 / 12);
tituloPrincipal.setDrawable(new TextureRegionDrawable(new TextureRegion(Texts.tituloPrincipal)));
tituloPrincipal.setSize(Texts.tituloPrincipal.getWidth()/3,
Texts.tituloPrincipal.getHeight()/3);
stage.addActor(tituloPrincipal);
//Play Button
botonPlay = new Image();
botonPlay.setPosition(Settings.SCREEN_WIDTH * 4 / 10, Settings.SCREEN_HEIGHT / 16);
botonPlay.setDrawable(new TextureRegionDrawable(new TextureRegion(Buttons.playbutton)));
botonPlay.setSize(Buttons.playbutton.getWidth()/16,
Buttons.playbutton.getHeight()/18);
stage.addActor(botonPlay);
//Song
Sounds.musicMainScreen.play();
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
botonPlay.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new GameScreen(game));
dispose();
}
});
stage.draw();
stage.act(delta);
}
@Override
public void resize ( int width, int height){
}
@Override
public void pause () {
}
@Override
public void resume () {
}
@Override
public void hide () {
}
@Override
public void dispose () {
}
}
游戏画面:
public class GameScreen implements Screen {
private Game game;
private Stage stage;
public GameScreen(Game game) {
this.game = game;
stage = new Stage();
Gdx.input.setInputProcessor(stage);
deleteMainScreenSources();
//song
Sounds.musicGameScreen.play();
}
public void deleteMainScreenSources() {
Sounds.musicMainScreen.stop();
}
@Override
public void show() {
}
@Override
public void render(float delta) {
}
@Override
public void resize ( int width, int height){
}
@Override
public void pause () {
}
@Override
public void resume () {
}
@Override
public void hide () {
}
@Override
public void dispose () {
}
}
按钮class:
public class Buttons {
public static Texture playbutton;
public static void load() {
playbutton = new Texture(Gdx.files.internal("playbutton.png"));
playbutton.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
}
public static void dispose() {
playbutton.dispose();
}
}
标题的class就像按钮的class。
您的 render
方法就是绘制屏幕的方法。如果您没有意识到,这是一种游戏循环,它会不停地执行(我相信默认情况下大约是 60 次/秒)。所以你所有的绘图和重绘都会 here.Usually ,你会先清除你的屏幕,然后在屏幕上绘制其他图像。 Libgdx 提供与 opengl 的接口以清除屏幕。就我而言,我总是在 render
方法的第一行清除屏幕并在其上绘制其他内容,以确保我的屏幕陈旧内容是 gone.In 您的 GameScreen
,你的 render
方法是空的,所以你之前的屏幕还没有被清除。我相信在 GameScreen 中添加 clear 和 drawing 您的新舞台将解决您的问题。
让您在 GameScreen 中的代码呈现类似这样的内容并尝试,
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
此外,出于同样的原因,我会尽量避免在“render”方法中创建任何新对象。菜单屏幕中的 addListener 方法可以进入它的构造函数。这样您就不会在每次游戏循环执行时都创建匿名侦听器。这可能不会对您当前的场景产生太大影响,但是如果您在游戏循环中以这种方式创建一些重物或一些 Sprites,则会大大拖累您的游戏性能。
此时我有 2 个屏幕:MainScreen
(这是游戏的主菜单)和 GameScreen
。我有一个 "button"(它是一个图像),当用户点击它时,MainScreen
切换到 GameScreen
。
问题是当第二个屏幕(按钮和主标题)出现时,MainScreen
的所有图像都没有消失。我已经为此工作了好几天了,我不知道还能做什么。
主要class游戏:
public class MyGame extends Game {
@Override
public void create () {
Sounds.load();
Texts.load();
Buttons.load();
setScreen(new MainScreen(this));
}
@Override
public void dispose () {
super.dispose();
Sounds.dispose();
Texts.dispose();
Buttons.dispose();
}
}
主屏幕:
public class MainScreen implements Screen {
private MyGame game;
private Stage stage;
private OrthographicCamera camera;
private Viewport viewport;
public static Image tituloPrincipal, botonPlay;
public MainScreen(final MyGame game) {
this.game = game;
//stage
camera = new OrthographicCamera(Settings.SCREEN_WIDTH, Settings.SCREEN_HEIGHT);
viewport = new StretchViewport(Settings.SCREEN_WIDTH, Settings.SCREEN_HEIGHT, camera);
stage = new Stage(viewport);
//background
stage.addActor(new Image(Sprites.mainBackground));
//Title of the game
tituloPrincipal = new Image();
tituloPrincipal.setPosition(Settings.SCREEN_WIDTH * 1 / 6, Settings.SCREEN_HEIGHT * 4 / 12);
tituloPrincipal.setDrawable(new TextureRegionDrawable(new TextureRegion(Texts.tituloPrincipal)));
tituloPrincipal.setSize(Texts.tituloPrincipal.getWidth()/3,
Texts.tituloPrincipal.getHeight()/3);
stage.addActor(tituloPrincipal);
//Play Button
botonPlay = new Image();
botonPlay.setPosition(Settings.SCREEN_WIDTH * 4 / 10, Settings.SCREEN_HEIGHT / 16);
botonPlay.setDrawable(new TextureRegionDrawable(new TextureRegion(Buttons.playbutton)));
botonPlay.setSize(Buttons.playbutton.getWidth()/16,
Buttons.playbutton.getHeight()/18);
stage.addActor(botonPlay);
//Song
Sounds.musicMainScreen.play();
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
botonPlay.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
game.setScreen(new GameScreen(game));
dispose();
}
});
stage.draw();
stage.act(delta);
}
@Override
public void resize ( int width, int height){
}
@Override
public void pause () {
}
@Override
public void resume () {
}
@Override
public void hide () {
}
@Override
public void dispose () {
}
}
游戏画面:
public class GameScreen implements Screen {
private Game game;
private Stage stage;
public GameScreen(Game game) {
this.game = game;
stage = new Stage();
Gdx.input.setInputProcessor(stage);
deleteMainScreenSources();
//song
Sounds.musicGameScreen.play();
}
public void deleteMainScreenSources() {
Sounds.musicMainScreen.stop();
}
@Override
public void show() {
}
@Override
public void render(float delta) {
}
@Override
public void resize ( int width, int height){
}
@Override
public void pause () {
}
@Override
public void resume () {
}
@Override
public void hide () {
}
@Override
public void dispose () {
}
}
按钮class:
public class Buttons {
public static Texture playbutton;
public static void load() {
playbutton = new Texture(Gdx.files.internal("playbutton.png"));
playbutton.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
}
public static void dispose() {
playbutton.dispose();
}
}
标题的class就像按钮的class。
您的 render
方法就是绘制屏幕的方法。如果您没有意识到,这是一种游戏循环,它会不停地执行(我相信默认情况下大约是 60 次/秒)。所以你所有的绘图和重绘都会 here.Usually ,你会先清除你的屏幕,然后在屏幕上绘制其他图像。 Libgdx 提供与 opengl 的接口以清除屏幕。就我而言,我总是在 render
方法的第一行清除屏幕并在其上绘制其他内容,以确保我的屏幕陈旧内容是 gone.In 您的 GameScreen
,你的 render
方法是空的,所以你之前的屏幕还没有被清除。我相信在 GameScreen 中添加 clear 和 drawing 您的新舞台将解决您的问题。
让您在 GameScreen 中的代码呈现类似这样的内容并尝试,
@Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
此外,出于同样的原因,我会尽量避免在“render”方法中创建任何新对象。菜单屏幕中的 addListener 方法可以进入它的构造函数。这样您就不会在每次游戏循环执行时都创建匿名侦听器。这可能不会对您当前的场景产生太大影响,但是如果您在游戏循环中以这种方式创建一些重物或一些 Sprites,则会大大拖累您的游戏性能。