LibGdx Textbutton Listener

时间:2015-12-18 20:03:30

标签: java libgdx

我完全是新的,我需要一个项目的快速帮助!!

如果有人知道的话,我会用LibGdx写一点“Risiko”。我创建一个菜单,我的TextButtons不工作!这是重要的部分:

stage = new Stage[3];
    stage[0] = new Stage();
    stage[1]= new Stage();
    stage[2]= new Stage();
    batch = new SpriteBatch();
    setBmap(new Texture("map.png"));

    inputMultiplexer=new InputMultiplexer();
    inputMultiplexer.addProcessor(this);

    inputMultiplexer.addProcessor(stage[0]);
    inputMultiplexer.addProcessor(stage[1]);
    inputMultiplexer.addProcessor(stage[2]);
    Gdx.input.setInputProcessor(inputMultiplexer);
    button = new TextButton[8];
    //Southamerica
    button[0] = new TextButton("Attack", textButtonStyle);
    button[0].setBounds(605,463, 100,40);

...创建按钮。

    stage[0].addActor(button[0]);
    stage[0].addActor(button[1]);
    stage[0].addActor(button[2]);
    stage[1].addActor(button[4]);
    stage[1].addActor(button[5]);
    stage[1].addActor(button[6]);
    stage[2].addActor(button[3]);
    stage[2].addActor(button[7]);

    for(int i= 0; i <= 7 ; i++){
        button[i].addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y){
                System.out.println("Button action");
            }});
        }
    /*button[0].addListener(new ChangeListener() {
        public void changed (ChangeEvent event, Actor actor) {
            System.out.println("BUTTOn");
        }
    });*/
        button[0].addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y){
                System.out.println("BUTTON PRESSED!!");
            }});

向舞台添加按钮并创建监听器!

现在我的渲染():

    super.render();
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);   

    font = new BitmapFont();
    batch.begin();
    batch.draw(getBmap(), 0, 0);
    stage[0].draw();

    -
        batch.end();     
    }

我使用不同的阶段确实绘制不同的菜单。仅供参考:D

已经感谢您的回答!!

1 个答案:

答案 0 :(得分:0)

抱歉,现在我收到了请求。我不知道我必须这样做:

inputMultiplexer=new InputMultiplexer();
inputMultiplexer.addProcessor(stage[0]);
inputMultiplexer.addProcessor(stage[1]);
inputMultiplexer.addProcessor(stage[2]);
inputMultiplexer.addProcessor(this);
Gdx.input.setInputProcessor(inputMultiplexer);

而不是这样:

inputMultiplexer=new InputMultiplexer();
inputMultiplexer.addProcessor(this);
inputMultiplexer.addProcessor(stage[0]);
inputMultiplexer.addProcessor(stage[1]);
inputMultiplexer.addProcessor(stage[2]);
Gdx.input.setInputProcessor(inputMultiplexer);

我喜欢它:D RIP