Sutty1000
Sutty1000

Reputation: 789

How to detect touch up after touch down

I am trying to figure out how to detect when a finger is lifted from the screen. Essentialy I want my program to perform an action once a finger is down until that same finger is up what I have so far does not detect once the finger goes up:

for(int i = 0; i < len; i++) {
    TouchEvent event = touchEvents.get(i);
    if(event.type == TouchEvent.TOUCH_DOWN) {
        while (event.type != TouchEvent.TOUCH_UP){
            if((event.y > 160) && ((world.batMan.getY() + world.batMan.getLength()) < 319)) {
                world.bat.moveSouth();
            }
            if((event.y < 160) && (world.batMan.getY() > 0)) {
                world.bat.moveNorth();
            }
            event = touchEvents.get(i);
        }
    }
}

Solved it although I think I have now made it more complex than needed!

for(int i = 0; i < len; i++) {

        TouchEvent event = touchEvents.get(i);



        if (event.type == TouchEvent.TOUCH_UP)
        {
            System.out.println("WOOT");
            touchNorth = false;
            touchSouth = false;
        }

        else {


            if((event.y > 160) && ((world.bat.getY() + world.bat.getLength()) < 319)
                    | (touchSouth == true))   {
                touchSouth = true;


            }

            if((event.y < 160) && (world.bat.getY() > 0) | (touchNorth == true)) {
                touchNorth = true;

            }

        }
    }


    if((touchSouth == true) && ((world.bat.getY() + world.bat.getLength()) < 319))  {
        world.bat.moveSouth();
        touchSouth = true;
    }

    if(touchNorth == true && (world.bat.getY() > 0)) {
        world.bat.moveNorth();
        touchNorth = true;

    }

Upvotes: 1

Views: 3900

Answers (1)

Caner
Caner

Reputation: 59228

You have an infinite loop:

while (event.type != TouchEvent.TOUCH_UP){

 ...

 event = touchEvents.get(i);
}

i is never incremented inside this loop. I'm surprised that your app is not closed by the system.

Upvotes: 3

Related Questions