我的程序中有两个分别命名为“ stagemain”和“ stage”和游戏画面的阶段,第一个stagemain出现,然后单击“新游戏”,游戏开始。角色死了,舞台到了。在第二阶段,有一个homebutton图像。我将其添加为clickListener,因此它导致了第一个屏幕(stagemain)...但是我遇到了一个问题。当我单击该homebutton时,第一个屏幕(stagemain)来了,突然,我首先单击按钮的第二个屏幕(stage)又出现了。我单击主页按钮,stagemain出现约0.5秒,然后返回到stagemain。我试图解释我的问题正在发生什么,希望我能成功..谢谢

这是homebutton图片的clickListener代码。在gameState = 0中,stagemain来了...在gameState = 2中,阶段来了。在addListener代码中,我编写了stagemain的首选项。

 Texture homeButtonTexture = new Texture(Gdx.files.internal("homebutton5.png"));
    Image homeButtonImage = new Image(homeButtonTexture);
    homeButtonImage.setPosition(4*Gdx.graphics.getWidth()/10,Gdx.graphics.getHeight()/4+Gdx.graphics.getHeight()/22);
    homeButtonImage.setSize(Gdx.graphics.getWidth()/12,Gdx.graphics.getHeight()/10);

    homeButtonImage.addListener(new ClickListener(){
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button){
            if(gameState == 2){
                gameState = 0;
                birdY = 2 * Gdx.graphics.getHeight() / 3;   // Oyunu tekrar başlatırsak kuşun yerini ayarlıyor

                Gdx.input.setInputProcessor(stagemain);

                Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
                Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                stagemain.act(Gdx.graphics.getDeltaTime());

                stagemain.getBatch().begin();                                                                                    // stagemain nin background u ayarlama
                stagemain.getBatch().draw(background,0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());               // stagemain nin background u ayarlama
                stagemain.getBatch().draw(bird, birdX, birdY, Gdx.graphics.getWidth()/15, Gdx.graphics.getHeight()/10);
                stagemain.getBatch().end();                                                                                     // stagemain nin background u ayarlama
                stagemain.draw();
            }
            return true;
        }
    });
    stage.addActor(homeButtonImage);

最佳答案

轻按按钮后,侦听器中的代码执行一次,而不是像渲染方法中那样连续执行。在侦听器内部绘制任何内容,仅更改状态是没有意义的。您的渲染循环应做出反应,并根据游戏状态如何绘制适当的屏幕。

10-04 10:18