我的手指跟着Bitmap的问题

时间:2015-12-29 23:29:45

标签: java android bitmap touch ontouchevent

我正在尝试创建一个位图,存储在类Player中,按照我的手指,而不是让它能够传送"传送"。我首先尝试了这段代码:

@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;
    }

但是我注意到,在点击时即使手指轻微移动也会导致Action_MOVE,所以我将ACTION_MOVE中的代码替换为:

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);
}

但是这导致玩家根本没有移动,当玩家在正确地跟随手指之前,它也会传送&#34;传送&#34;如果我轻拍球员。我怎样才能摆脱传送&#34;行为正常吗?

1 个答案:

答案 0 :(得分:1)

使用ACTION_MOVE是正确的,但是当pointerX设置为等于event.getX()时,你将event.getX()与pointerX进行比较。你的意思是将它与player.getX()进行比较吗?