Reputation: 417
I have a screen with 8 different static buttons on it. I have written a class to make this easy:
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;
}
}
}
As you can see in the code above, I am writing out a message to the LogCat, but I have been unable to work out how to determine which of the buttons was pressed.
In my game, a bit like "simon", I wish to display a colour or shape, and then get the player to click the correct colour matching button - so I will need to know which button they have pressed.
My buttons are instantiated as follows, my main Game Screen which Implements Screen.
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);
Any help would be appreciated here.
How can I listen in my main game class for these button touch events, and determine which one was pressed?
Many Thanks
James
Upvotes: 0
Views: 113
Reputation: 206
You could pass a identifier index as a parameter when creating your TouchButton object.
Then when a button is pressed simply set the static int lastPressIndex to the index of the button being pressed.
public TouchButton(String argButtonID, float argActorX, float argActorY, int argWidth, int argHeight, int buttonIndex)
Define a static variable representing the button that was pressed:
public static int lastPressIndex = 0;
Then in your game loop (render method) you could check the value of lastButtonPressIndex and see which button pass pressed by it's index.
Your TouchButton class would look something like this:
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;
}
}
}
When creating your TouchButton instances you simply keep track of which button belongs to which index. A HashMap would be the way to go.
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.
in your render method:
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.
}
}
Upvotes: 1