多个 Actor 作为按钮 - 检测哪一个是 touched/clicked

Multiple Actors as Buttons - detecting which one is touched/clicked

我有一个屏幕,上面有 8 个不同的静态按钮。我写了一个 class 来简化这件事:

public class TouchButton extends Actor{
    Texture texture;
    float actorX, actorY;
    int actorWidth, actorHeight;
    public boolean started = false;

    public TouchButton(String argButtonID, float argActorX, float argActorY, int argWidth, int argHeight){

        String file = argButtonID;
        texture = new Texture(Gdx.files.internal(file));
        this.actorX = argActorX;
        this.actorY = argActorY;
        this.actorWidth = argWidth;
        this.actorHeight = argHeight;

        setBounds(actorX, actorY, argWidth, argHeight);
        addListener(new InputListener(){
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                ((TouchButton)event.getTarget()).started = true;

                Gdx.app.debug("BUTTON", "pressed" );

                Gdx.input.vibrate(100);
                return true;
            }
        });
    }

    @Override
    public void draw(Batch batch, float alpha){
         batch.draw(texture,actorX,actorY,this.actorWidth, this.actorHeight);
    }

    @Override
    public void act(float delta){
        if(started){
            actorX+=5;
        }
    }
}

如您在上面的代码中所见,我正在向 LogCat 写出一条消息,但我一直无法弄清楚如何确定按下了哪个按钮。

在我的游戏中,有点像 "simon",我希望显示一种颜色或形状,然后让玩家点击正确的颜色匹配按钮 - 所以我需要知道他们有哪个按钮按下。

我的按钮实例化如下,我的主游戏画面实现了画面。

    purpleButton = new TouchButton("shapes/purple.jpg", 0.0f, stage.getHeight() - 256, 256, 256);
    purpleButton.setTouchable(Touchable.enabled);
    stage.addActor(purpleButton);

    pinkButton = new TouchButton("shapes/pink.jpg", stage.getWidth() - 256, 0.0f, 256, 256);
    pinkButton.setTouchable(Touchable.enabled);
    stage.addActor(pinkButton);

如有任何帮助,我们将不胜感激。

如何在我的主游戏 class 中监听这些按钮触摸事件,并确定按下了哪一个?

非常感谢

詹姆斯

您可以在创建 TouchButton 对象时将标识符索引作为参数传递。

然后当按下按钮时,只需将静态 int lastPressIndex 设置为被按下按钮的索引。

public TouchButton(String argButtonID, float argActorX, float argActorY, int argWidth, int argHeight, int buttonIndex)

定义一个表示按下的按钮的静态变量:

public static int lastPressIndex = 0;

然后在您的游戏循环(渲染方法)中,您可以检查 lastButtonPressIndex 的值并查看它的索引按下了哪个按钮。

您的 TouchButton class 看起来像这样:

public class TouchButton extends Actor{
    Texture texture;
    float actorX, actorY;
    int actorWidth, actorHeight;
    public boolean started = false;
    private int buttonIndex = 0; // New stuff

    public static int lastPressIndex = 0; // New stuff/

    public TouchButton(String argButtonID, float argActorX, float argActorY, int argWidth, int argHeight, int buttonIndex){ // new parameter

        String file = argButtonID;
        texture = new Texture(Gdx.files.internal(file));
        this.actorX = argActorX;
        this.actorY = argActorY;
        this.actorWidth = argWidth;
        this.actorHeight = argHeight;
        this.buttonIndex = buttonIndex; // new stuff

        setBounds(actorX, actorY, argWidth, argHeight);
        addListener(new InputListener(){
            public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
                ((TouchButton)event.getTarget()).started = true;

                Gdx.app.debug("BUTTON", "pressed" );
                TouchButton.lastPressIndex = this.buttonIndex; // new stuff
                Gdx.input.vibrate(100);
                return true;
            }
        });
    }

    @Override
    public void draw(Batch batch, float alpha){
         batch.draw(texture,actorX,actorY,this.actorWidth, this.actorHeight);
    }

    @Override
    public void act(float delta){
        if(started){
            actorX+=5;
        }
    }
}

创建 TouchButton 实例时,您只需跟踪哪个按钮属于哪个索引即可。 HashMap 将是可行的方法。

HashMap<Integer, TouchButton) holdsButtons = new HashMap<Integer, TouchButton>();

holdsButtons.put(1, new TouchButton("shapes/purple.jpg", 0.0f, stage.getHeight() - 256, 256, 256, 1)); // Created the TouchButton and made a relation between the object and an Integer value, in this case 1.

在您的渲染方法中:

public void render() {
   if(TouchButton.lastPressIndex > 0) { // Making sure a button is actually pressed.
        TouchButton pressedButton = holdsButtons.get(TouchButton.lastPressIndex);
        // Now you know which button was pressed.
        TouchButton.lastPressIndex = 0; // Reset the value.
    }
}