LibGDX中的按钮监听器

时间:2015-01-13 05:41:50

标签: android button libgdx

我的屏幕上有近100个按钮。我是否需要对这100个按钮执行相同操作,或者有任何简单的方法来实现此目的。 Pelase建议

button1.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
        Gdx.app.log("my app", "Pressed"); //** Usually used to start Game, etc. **//
        return true;
}

public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
        Gdx.app.log("my app", "Released");
} 

1 个答案:

答案 0 :(得分:0)

//添加变量类

ArrayList<Button> buttonArray = ArrayList<Button>();

InputListener inputListener;

在您的代码中添加show,例如:

您的代码中

//:

    inputListener = new InputListener() {

        public boolean touchDown (InputEvent event, float x, float y, 
                                  int pointer, int button) {

            //** Usually used to start Game, etc. **//
            Gdx.app.log("my app", "Pressed"); 
            return true;
        }

        public void touchUp (InputEvent event, float x, float y, 
                             int pointer, int button) {

            Gdx.app.log("my app", "Released");
        } 

    };
您的代码中

//:

buttonArray.add(button1);
buttonArray.add(button2);
buttonArray.add(button3);

for(int a = 0; a < buttonArray.size(); a++){
    buttonArray.get(a).addListener(inputListener);
}

它现在不会在Ide中测试,但我认为你可以提供帮助。

我不知道您想知道按下了哪个按钮,以便您可以进行一些更改,将其添加到响应中,但是如果它是最好的方式则不会。

//添加变量类

ArrayList<Button> buttonArray = ArrayList<Button>();

//在您的课程中添加:

private class TestInputListenerOverflow extends InputListener{

    int id;

    public TestInputListenerOverflow (int hashCode){
        this.id = hashCode;
    }
    public void setButtonId(int hashCode){
        this.id = hashCode;
    }
    public int getButtonId(){
        return id;
    }
    public boolean touchDown (InputEvent event, float x, float y, 
            int pointer, int button) {

        //** Usually used to start Game, etc. **//

        if(button1.hashCode() == id){
            Gdx.app.log("my app", "Pressed");
            Gdx.app.log("my app", "You Pressed button1");
        }else if(button2.hashCode() == id){
            Gdx.app.log("my app", "Pressed");
            Gdx.app.log("my app", "You Pressed button2");
        }else if(button3.hashCode() == id){
            Gdx.app.log("my app", "Pressed");
            Gdx.app.log("my app", "You Pressed button3");
        }else if(button4.hashCode() == id){
            Gdx.app.log("my app", "Pressed");
            Gdx.app.log("my app", "You Pressed button4");
        }else if(button5.hashCode() == id){
            Gdx.app.log("my app", "Pressed");
            Gdx.app.log("my app", "You Pressed button5");
        }

        return true;
        }

        public void touchUp (InputEvent event, float x, float y, 
                       int pointer, int button) {

        Gdx.app.log("my app", "Released");
        } 
}
您的代码中

//:

buttonArray.add(button1); 
buttonArray.add(button2);
buttonArray.add(button3);
buttonArray.add(button4);
buttonArray.add(button5);

for(int a = 0; a < buttonArray.size(); a++){
    buttonArray.get(a).addListener(
                        new TestInputListenerOverflow(
                        buttonArray.get(a).hashCode())
                        );
}