OnTouchEvent:我想在屏幕上用另一根手指移动时识别触摸
OnTouchEvent: I want to recognize touches while moving with an another finger on the screen
我正在尝试开发自己的游戏。但是我对 onTouchEvent-Method 有疑问。在显示我的代码之前很少解释。我希望能够用手指控制飞行。它应该总是跟随我的手指。为此,我实施了 MotionEvent.ACTION_MOVE 案例。航班只能在屏幕左侧移动。当我触摸屏幕右侧时,它应该会发射子弹。并且应该可以同时飞行和射击。这是我的代码:
public boolean onTouchEvent(MotionEvent event) {
int maskedaction= event.getActionMasked();
switch (maskedaction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
if (event.getX() > screenX / 2 && flight.toShoot == 0) {
flight.toShoot++;
}
break;
case MotionEvent.ACTION_MOVE: {
if (event.getX() < screenX / 2 - 100) {
flight.x = (int) event.getX() - 100;
flight.y = (int) event.getY() - 50;
}
break;
}
}
return true;
}
这就是代码。当我触摸屏幕的左半部分时,我可以驾驶飞机飞来飞去。当我触摸右侧时,我可以射击。但我不能同时做。我想先触摸左半边然后飞来飞去。通过单击右侧,同时我将手指移动到左侧,它应该会发射子弹。
我发现当我用一根手指同时点击两侧时,飞机就会发射一发子弹。如果我现在松开右手指(触摸屏幕时发射的手指),我可以飞来飞去,当我飞来飞去的时候,我可以同时射击。
您可以通过检测 ID 来检测您的触摸,https://developer.android.com/training/gestures/multi
你可以这样做:
int leftSideId = 0;
int rightSideId = 1;
public boolean onTouchEvent(MotionEvent event) {
int maskedaction= event.getActionMasked();
// Get the pointer ID
int mActivePointerId = event.getPointerId(0);
switch (maskedaction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
if (event.getX() > screenX / 2) {
rightSideId = mActivePointerId;
if(flight.toShoot == 0){
flight.toShoot++;
}
}else{
leftSideId = mActivePointerId;
}
break;
case MotionEvent.ACTION_MOVE: {
if (event.getX() < screenX / 2 - 100) {
if (mActivePointerId == leftSideId) {
flight.x = (int) event.getX() - 100;
flight.y = (int) event.getY() - 50;
}
}
break;
}
}
return true;
}
所以我自己找到了解决办法
public boolean onTouchEvent(MotionEvent event){
int amount=event.getPointerCount();
for (int i=0; i<amount; i++) {
float x = event.getX(i);
float y = event.getY(i);
if (x>screenX/2 && flight.toShoot==0){
flight.toShoot++;
}
if (x<screenX/2 -100 && flyingenabled)
{
flight.x = (int) x - 100;
flight.y = (int) y - 50;
}
}
return true;
}
这就是我的问题。比我想象的容易。
我正在尝试开发自己的游戏。但是我对 onTouchEvent-Method 有疑问。在显示我的代码之前很少解释。我希望能够用手指控制飞行。它应该总是跟随我的手指。为此,我实施了 MotionEvent.ACTION_MOVE 案例。航班只能在屏幕左侧移动。当我触摸屏幕右侧时,它应该会发射子弹。并且应该可以同时飞行和射击。这是我的代码:
public boolean onTouchEvent(MotionEvent event) {
int maskedaction= event.getActionMasked();
switch (maskedaction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
if (event.getX() > screenX / 2 && flight.toShoot == 0) {
flight.toShoot++;
}
break;
case MotionEvent.ACTION_MOVE: {
if (event.getX() < screenX / 2 - 100) {
flight.x = (int) event.getX() - 100;
flight.y = (int) event.getY() - 50;
}
break;
}
}
return true;
}
这就是代码。当我触摸屏幕的左半部分时,我可以驾驶飞机飞来飞去。当我触摸右侧时,我可以射击。但我不能同时做。我想先触摸左半边然后飞来飞去。通过单击右侧,同时我将手指移动到左侧,它应该会发射子弹。
我发现当我用一根手指同时点击两侧时,飞机就会发射一发子弹。如果我现在松开右手指(触摸屏幕时发射的手指),我可以飞来飞去,当我飞来飞去的时候,我可以同时射击。
您可以通过检测 ID 来检测您的触摸,https://developer.android.com/training/gestures/multi
你可以这样做:
int leftSideId = 0;
int rightSideId = 1;
public boolean onTouchEvent(MotionEvent event) {
int maskedaction= event.getActionMasked();
// Get the pointer ID
int mActivePointerId = event.getPointerId(0);
switch (maskedaction) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
if (event.getX() > screenX / 2) {
rightSideId = mActivePointerId;
if(flight.toShoot == 0){
flight.toShoot++;
}
}else{
leftSideId = mActivePointerId;
}
break;
case MotionEvent.ACTION_MOVE: {
if (event.getX() < screenX / 2 - 100) {
if (mActivePointerId == leftSideId) {
flight.x = (int) event.getX() - 100;
flight.y = (int) event.getY() - 50;
}
}
break;
}
}
return true;
}
所以我自己找到了解决办法
public boolean onTouchEvent(MotionEvent event){
int amount=event.getPointerCount();
for (int i=0; i<amount; i++) {
float x = event.getX(i);
float y = event.getY(i);
if (x>screenX/2 && flight.toShoot==0){
flight.toShoot++;
}
if (x<screenX/2 -100 && flyingenabled)
{
flight.x = (int) x - 100;
flight.y = (int) y - 50;
}
}
return true;
}
这就是我的问题。比我想象的容易。