java - libgdx:切换屏幕时图像不会消失

标签 java android libgdx

此时我有两个屏幕:MainScreen(它是游戏的主菜单)和GameScreen。我有一个“按钮”(它是一个图像),当用户单击它时,MainScreen 切换到 GameScreen

问题是,当第二个屏幕出现时,MainScreen 的所有图像都不会消失(按钮和主标题)。我已经为此工作好几天了,但我不知道还要做什么。

游戏的主要类别:

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

    }
}

按钮类:

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

}

标题的类就像按钮的类。

最佳答案

您的 render 方法用于绘制屏幕。如果您不知道,这是一种游戏循环,它会不间断地执行(我相信默认情况下约为 60/秒)。所以你所有的绘图和重绘都在这里。通常,你会首先清除屏幕,然后在屏幕上绘制其他图像。 Libgdx 提供与 opengl 的接口(interface)以清除屏幕。就我而言,我总是在 render 方法的第一行清除屏幕,并在其上绘制其他内容,以确保屏幕上的陈旧内容消失。在您的 GameScreen 中,您的 render 方法为空,因此您之前的屏幕尚未被清除。我相信在 GameScreen 中添加清晰并绘制新舞台将解决您的问题。

让你的代码在 GameScreen 中呈现类似这样的内容并尝试,

 @Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();

}

此外,出于同样的原因,我会尝试避免在“render”方法中创建任何新对象。菜单屏幕中的 addListener 方法可以进入其构造函数。这样您就不会在每个游戏循环执行中创建匿名监听器。在您当前的场景中,这可能不会受到太大影响,但如果您在游戏循环中以这种方式创建一些重物体或一些 Sprite ,这可能会严重拖累您的游戏性能。

关于java - libgdx:切换屏幕时图像不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50045647/

相关文章:

java - 在linux中通过java代码消费来自windows共享的数据

java - 没有 Hibernate Session 绑定(bind)到线程,并且配置不允许在此处创建非事务性 session

java - 为什么 Room 不从 Assets 中填充数据库?

android - 在代码中旋转一个按钮(或里面的文字)

java - 类(class)成员是什么意思? In-keepclassmembers Proguard

java - 想要将我的 ListView 与 Android Studio 中的文本链接

javascript - 网页 View 不加载网页图像

java - 在 Drawable 类中使用 onSizeChanged

java - Libgdx:SpriteBatch.draw spriteVertecies 奇怪的 UV 坐标行为

java - 无法弄清楚如何使用 isAnimationFinished() 方法(LibGDX)