问题描述
我是一名学生开发一个空气曲棍球Android游戏。
我有理解多点触摸的问题。
我刚刚得知ACTION_DOWN,ACTION_POINTER_DOWN等。
i am a student developing an air hockey android games.i am having a problem with understanding multi touch.i just learns about ACTION_DOWN, ACTION_POINTER_DOWN etc.
但问题是在ACTION_MOVE。
我创建2个精灵2 player.1st精灵将移动在我的手指第一次去,但我的第二个精灵不动的地方我的第二个手指移动。
but by problem is at ACTION_MOVE.i create 2 sprite for 2 player.1st sprite will move where my 1st finger go, but my 2nd sprite doesn't move where my 2nd finger move.
我的问题是,我多么想确定哪个手指在移动ACTION_MOVE?我曾尝试使用getPointerId(指数),但我不知道如何使用它,因为该指数是不断变化的,如果第一手指离开屏幕
my question is, how i want to identified which finger is moving in ACTION_MOVE? i have tried to use getPointerId(index), but i am not understand how to use it because the index is changing if the 1st finger leave the screen
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
break;
}
case MotionEvent.ACTION_POINTER_DOWN: {
break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_POINTER_UP: {
break;
}
case MotionEvent.ACTION_MOVE: {
if((int)event.getPointerId(index) == 0){ //i know this IF statement is wrong, what should i do?
player1.setX((int)event.getX()); //player1 & player2 is a sprite object
player1.setY((int)event.getY());
}
if((int)event.getPointerId(index) == 1){
player1.setX((int)event.getX());
player1.setY((int)event.getY());
}
}
}
推荐答案
您无法知道哪些领域将首先感动。第一手指/触摸获取指针的id = 0的第二= 1等。你可以做的是这样的:
You cannot know which area will be touched first. The first finger/ touch gets pointer id = 0 the second = 1 and so on. What you can do is this:
public boolean onTouchEvent(MotionEvent event) {
// If this does not work search for a way to get the screen width
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int eventaction = event.getActionMasked();
int num = event.getPointerCount();
// For every touch
for (int a = 0; a < num; a++) {
int X = (int) event.getX(event.getPointerId(a));
int Y = (int) event.getY(event.getPointerId(a));
int allowed_touch_range = display.getWidth() / 2; // Your screen width divided by 2
switch (eventaction) {
case MotionEvent.ACTION_MOVE:
// Left half of the screen
if (X < allowed_touch_range) {
/* Player1 Move */
}
// Rigth half
if (X > allowed_touch_range) {
/* Player2 Move */
}
break;
}
}
return true;
}
这篇关于在确定ACTION_MOVE多点触控指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!