跟随我手指的位图问题
Problems with Bitmap following my finger
我正在尝试制作一个位图,存储在 class 播放器中,跟随我的手指四处走动,但不赋予它 "teleport" 的能力。我首先尝试了这段代码:
@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);
}
但这导致玩家根本不动,之前玩家会正确地跟随手指,但如果我从玩家那里轻敲它也会 "teleport"。我怎样才能正确摆脱 "teleporting" 行为?
您使用 ACTION_MOVE 是正确的,但是当 pointerX 设置为 event.getX() 时,您将 event.getX() 与 pointerX 进行比较。您是要将其与 player.getX() 进行比较吗?
我正在尝试制作一个位图,存储在 class 播放器中,跟随我的手指四处走动,但不赋予它 "teleport" 的能力。我首先尝试了这段代码:
@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);
}
但这导致玩家根本不动,之前玩家会正确地跟随手指,但如果我从玩家那里轻敲它也会 "teleport"。我怎样才能正确摆脱 "teleporting" 行为?
您使用 ACTION_MOVE 是正确的,但是当 pointerX 设置为 event.getX() 时,您将 event.getX() 与 pointerX 进行比较。您是要将其与 player.getX() 进行比较吗?