gpt4 book ai didi

java - 多个 Actor 作为按钮 - 检测哪个 Actor 被触摸/点击

转载 作者:行者123 更新时间:2023-12-01 11:53:56 24 4
gpt4 key购买 nike

我的屏幕上有 8 个不同的静态按钮。我编写了一个类来简化此操作:

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);

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

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

非常感谢

詹姆斯

最佳答案

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

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

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

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

public static int lastPressIndex = 0;

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

您的 TouchButton 类看起来像这样:

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.
}
}

关于java - 多个 Actor 作为按钮 - 检测哪个 Actor 被触摸/点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28584133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com