我的屏幕上有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);
这里有任何帮助。
我如何在主游戏课程中收听这些按键触摸事件,并确定哪一个按下了?
非常感谢
詹姆斯
答案 0 :(得分:1)
创建TouchButton对象时,可以将标识符索引作为参数传递。
然后按下按钮时,只需将static 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.
}
}