user2712795
user2712795

Reputation: 65

Keeping button as pressed when other buttons are unpressed

I have three buttons that are images on the screen. When the user presses on the screen within the area of the button, the boolean value saying the button is pressed turns to true. This works great except I have three buttons so when I hold down two buttons and lift up one, all the buttons turn to as though I never pressed them. I know where to resolve this but I'm not sure how. How can I fix this so that, if I have two buttons pressed down, when I lift up my finger from one button, the other button doesn't become "not pressed" but remains "pressed"?

boolean B1Pressed = false;
boolean B2Pressed = false;
boolean B3Pressed = false;

int len = touchEvents.size();
 for (int i = 0; i < len; i++) {
 TouchEvent event = touchEvents.get(i);

 if (event.type == TouchEvent.TOUCH_DOWN) {

 if (inBounds(event, 340,512,140,140)) {
  B1Pressed = true;
  Log.d("GameScreen", "Button 1 Pressed");
 // Button 1
 }

 if (inBounds(event, 340,320,140,140)) {
  B2Pressed = true;
  Log.d("GameScreen", "Button 2 Pressed");
 // Button 2
 }

 if (inBounds(event, 340,120,140,140)) {
  B3Pressed = true;
  Log.d("GameScreen", "Button 3 Pressed");
 // Button 3
 }

 }

 if (event.type == TouchEvent.TOUCH_UP) {

 B1Pressed = false;
 B2Pressed = false;
 B3Pressed = false;
}   
private boolean inBounds(TouchEvent event, int x, int y, int width,
        int height) {
    if (event.x > x && event.x < x + width - 1 && event.y > y
            && event.y < y + height - 1)
        return true;
    else
        return false;
}

Upvotes: 0

Views: 254

Answers (2)

Anudeep Bulla
Anudeep Bulla

Reputation: 8568

This seems to be a round about way to accomplish this. But if however, this is how you wanna do it, maybe just toggling the boolean flag will accomplish what you need.

Just change the first if condition to

if (event.type == TouchEvent.TOUCH_UP || event.type == TouchEvent.TOUCH_DOWN ) {

and when ever you are setting the boolean flat, invert it. Like so,

BxPressed = !BxPressed;

Where you would substitute the x with the numbers 1-3.

You can then remove the second if to handle the UP event altogether.

Upvotes: 1

RScottCarson
RScottCarson

Reputation: 980

This is the problem:

if (event.type == TouchEvent.TOUCH_UP) {

    B1Pressed = false;
    B2Pressed = false;
    B3Pressed = false;
}   

Anytime a TouchEvent.TOUCH_UP event is registered, all buttons are released. Try using the same logic to determine which buttons have been pressed to determine which button has been released.

if (event.type == TouchEvent.TOUCH_UP) {

 if (inBounds(event, 340,512,140,140)) {
     B1Pressed = false;
     Log.d("GameScreen", "Button 1 Released");
     // Button 1
 }

 if (inBounds(event, 340,320,140,140)) {
     B2Pressed = false;
     Log.d("GameScreen", "Button 2 Released");
     // Button 2
 }

 if (inBounds(event, 340,120,140,140)) {
     B3Pressed = false;
     Log.d("GameScreen", "Button 3 Released");
 // Button 3
 }

 }

Upvotes: 3

Related Questions