Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

OK so I know how to iterate through the different pointers that are currently touching the screen so I know what the coordinates are of these pointers using the following:

Code snippet

case MotionEvent.ACTION_MOVE:{

    //Count through all the pointers currently touching the screen
    for(int index=0; index<event.getPointerCount(); index++) {

     //Is finger on the left button? If so, then go (or continue going) left
    if (event.getY(index) > renderer.leftButton("top") && event.getX(index)<renderer.leftButton("right")){
                renderer.setSpritewState('l', true);
                renderer.setSpriteState('r', false);}

 //Or is finger on the right button? If so, then go (or continue going) right
            else if (event.getY(index)>renderer.rightButton("top") && event.getX(index)>renderer.rightButton("left") && event.getX(index)<renderer.rightButton("right")){
                renderer.setSpriteState('l', false);
                renderer.setSpriteState('r', true);}
            }
            break;          


}

However, this is constantly triggering one of the two conditions I guess as it's picking up minute movement of the finger/s. And, more importantly, if there is a finger on each 'button' - the sprite goes a bit crazy as each condition fights with the other and constantly flags the sprite to move left and then right etc.

What I'm really after is this something like this:

enter image description here

So, if a finger moves within a specified area, nothing happens as it's counter-productive. But if a finger moves from one area to another, then the action is triggered.

I'm guessing the code for this would need to save the old position of each of the fingers to compare to the new positions, but I'm not really sure how to go about this. Help would be appreciated. Thanks.

share|improve this question

1 Answer 1

You can specify the areas by a simple integer (for now). Area A would have index 1, and area B index 2.

You will store the last 'tapped' area in a variable. When the finger is released, and the 'released' - where the pointer up event triggered - area is different than the 'tapped' one, you trigger the action.

If you want to know when the touch left a specific area, then you'll need to have a list of Rectangles with the respective areas. Just do a simple Contains check to see if the moved point is inside the same area where it 'tapped' the first time.

Something like this (pseudocode):

// somewhere globally/locally
var initialArea = -1

...

var list = new list({0,0,20,30}, {20, 0, 20, 30}) // the rectangles list
if (event(tap) && initialArea == -1)
    initialArea = findArea(list, new point(tap.x, tap.y))

if (event(release_pointer))
    initialArea = -1; // no area at the moment

if (event(pointer_move) && initialArea != -1)
    currentArea = findArea(list, new point(tap.x, tap.y))
    if (currentArea != initialArea)
        // trigger action!
        initialArea = currentArea // prevent multiple firings.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.