首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >作为按钮的多个行为体-检测哪个被触摸/点击

作为按钮的多个行为体-检测哪个被触摸/点击
EN

Stack Overflow用户
提问于 2015-02-18 12:50:46
回答 1查看 109关注 0票数 0

我有一个屏幕上有8个不同的静态按钮。我写了一个课程让这件事变得简单:

代码语言:javascript
复制
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写一条消息,但我一直无法确定如何确定按下了哪个按钮。

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

我的按钮实例化如下,我的主游戏屏幕实现屏幕。

代码语言:javascript
复制
    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);

如果能提供任何帮助,我们将不胜感激。

我如何在我的主要游戏课上听这些按钮触摸事件,并确定哪个是按下的?

非常感谢

詹姆斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-18 15:25:05

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

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

代码语言:javascript
复制
public TouchButton(String argButtonID, float argActorX, float argActorY, int argWidth, int argHeight, int buttonIndex)

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

代码语言:javascript
复制
public static int lastPressIndex = 0;

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

您的TouchButton类应该如下所示:

代码语言:javascript
复制
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应该是最好的选择。

代码语言:javascript
复制
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.

在您的渲染方法中:

代码语言:javascript
复制
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.
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28584133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档