舞台不会改变为另一个舞台

时间:2018-02-26 08:09:34

标签: java libgdx

我有2个阶段,名为" stagemain"和"阶段"和我的程序中的gamescreen,第一阶段来了,我点击"新游戏",然后游戏开始。人物死亡和阶段来临。在第二阶段,有一个家庭按钮图像。我添加了clickListener,所以它导致第一个屏幕(stagemain)...但我遇到了一个问题。点击那个homebutton的时候,第1个画面(stagemain)来了,突然第2个画面(舞台),我先点击按钮,再次出现。我点击homebutton,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);

1 个答案:

答案 0 :(得分:0)

当点击按钮时,侦听器中的代码会执行一次,而不是像渲染方法那样连续执行。在侦听器中绘制任何内容是没有意义的,只是为了改变状态。渲染循环应该根据游戏状态做出反应并绘制适当的屏幕。