Tirth Allspark Rami
Tirth Allspark Rami

Reputation: 99

Problems with Bitmap following my finger

I am trying to make a bitmap, stored in the class Player, follow my finger around without giving it the ability to "teleport". I first tried this block of code:

@Override
public boolean onTouchEvent(MotionEvent event) {

    pointerX = (int)event.getX();
    pointerY = (int)event.getY();
    switch (event.getAction()) {

        case MotionEvent.ACTION_DOWN:

            pointerX = (int)event.getX();
            pointerY = (int)event.getY();
            if (player.playerAlive) {
                if((event.getX() > player.getX() || event.getX() < player.getX() + player.getWidth()) &&
                   (event.getY() > player.getY() || event.getY() < player.getY() + player.getHeight())) {
                    isDown = true;
                }
            }

            break;

        case MotionEvent.ACTION_MOVE:
                if(isDown){
                    player.move(true);
                }

            break;

        case MotionEvent.ACTION_UP:
            isDown = false;
            break;
    }
    return true;
    }

However I noticed that even the slightest shift of the finger when tapping will result in Action_MOVE, so I replaced the code in ACTION_MOVE to this:

if(isDown){
    if(event.getX() < pointerX - 20 || event.getX() > pointerX + 20)
        player.move(true);
    if(pointerY < event.getY() - 20 || pointerY > event.getY() + 20)
        player.move(true);
}

But this resulted in the player not moving at all, when before the player would follow the finger properly but it would also "teleport" if I tapped away from the player. How can I get rid of the "teleporting" behavior properly?

Upvotes: 0

Views: 38

Answers (1)

TheoKanning
TheoKanning

Reputation: 1639

You're correct in using ACTION_MOVE, but you're comparing event.getX() to pointerX when pointerX is set equal to event.getX(). Did you mean to compare it to player.getX()?

Upvotes: 1

Related Questions