libGDX在侦听器之间更改

格子衬衫

我有一个libGDX基础的游戏,其中2个Stages用于存储元素。有ImageButtons和Sprites。所有Sprites都应调用相同的函数,因此它们位于上StageImageButtons位于另一个上Stage我使用MENU按钮来在听众之间切换。

@Override
public boolean keyDown(int keycode) {
    if ((keycode == Keys.M) || (keycode == Keys.MENU)) {
       // some code
}

已经有两个带有不同按钮的不同阶段。我在keyDown函数内部在它们之间进行切换,就像这样。这个函数在里面keyDown

ExitButton.addListener(new ClickListener() {
 @Override
   public void clicked(InputEvent event, float x, float y) {
   Gdx.input.setInputProcessor(dialog);
   pause();
   }
});

因此,当调用其他功能时,我将后监听器设置为默认值,而不是主要游戏功能。我想将其设置为ToggleButton,可以在听众之间进行切换。这似乎很简单,但是我不能为此目的使用第三种类型的侦听器。

问题是,这不是MENU大多数较新设备上的任何按钮。

新答案:

根据您的评论,我假设您想在两个阶段之间进行切换,同时使第三阶段始终处于活动状态。这里的关键是“InputMultiplexer可能有更漂亮的方法”,但是我认为类似的方法应该起作用:

public class ButtonTest implements Screen
{
    public class RedCircle extends Actor
    {
         //just a placeholder, your implementation would probably not use this
    }

    public class BlackCircle extends Actor
    {
        //just a placeholder, your implementation would probably not use this
    }

    InputMultiplexer plexer;
    Stage hudStage;
    Stage redStage;
    Stage blackStage;
    public ButtonTest(Skin skin,List<RedCircle> redCircles, List<BlackCircle> blackCircles)
    {
        hudStage = new Stage();
        redStage = new Stage();
        blackStage = new Stage();
        plexer = new InputMultiplexer();
        plexer.addProcessor(hudStage);
        final TextButton switchButton = new TextButton("switch",skin);
        switchButton.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y)
            {
                if(switchButton.isChecked())
                {
                    plexer.removeProcessor(redStage);
                    plexer.addProcessor(blackStage);
                }
                else
                {
                    plexer.addProcessor(redStage);
                    plexer.removeProcessor(blackStage);
                }
                switchButton.setText("this button is clicked");
                super.clicked(event, x, y);
            }
        });

        for(RedCircle r : redCircles)
            redStage.addActor(r);

        for(BlackCircle b : blackCircles)
            blackStage.addActor(b);


        switchButton.setPosition(0.1f* Gdx.graphics.getWidth(),0.1f*Gdx.graphics.getHeight());
        hudStage.addActor(switchButton);
    }

    @Override
    public void show()
    {
        Gdx.input.setInputProcessor(plexer);

    }

    @Override
    public void render(float delta)
    {
        Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        hudStage.act();
        hudStage.draw();
        redStage.act();
        redStage.draw();
        blackStage.act();
        blackStage.draw();
    }

    @Override
    public void resize(int width, int height)
    {

    }

    @Override
    public void pause()
    {

    }

    @Override
    public void resume()
    {

    }

    @Override
    public void hide()
    {

    }

    @Override
    public void dispose()
    {
        redStage.dispose();
        blackStage.dispose();
        hudStage.dispose();
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章