gpt4 book ai didi

java - Android 多点触控 : Not detecting MotionEvent. ACTION_POINTER_DOWN

转载 作者:行者123 更新时间:2023-12-01 23:08:00 24 4
gpt4 key购买 nike

这是我的代码的一部分。我不明白为什么没有检测到 MotionEvent.ACTION_POINTER_DOWN (第二根手指触摸屏幕)。有人可以解释为什么这不起作用吗?

单人游戏运行良好。两个玩家(多点触控)则不然。

什么对两个玩家有效- 当用户仅按下屏幕的任一半时,它会调用 goUp()

什么对两个玩家不起作用- 当用户按下屏幕的一半,然后按下另一半时,另一半不会调用 goUp()

另外,有没有办法在模拟器上同时模拟多个触摸,以便我可以在 logcat 中看到 log

private int pointerId = -1, pointerId2 = -1;

@Override
public boolean onTouch(View view, MotionEvent event) {

// Depending on the touch, depends on the direction of the ricer (up or down)
if(!TWO_PLAYER_MODE){
switch (event.getActionMasked()) {

// When finger touches the screen, ricer will accelerate up
case MotionEvent.ACTION_DOWN:
ricerView.goUp();
break;

// When finger is lifted from the screen, ricer will accelerate down
case MotionEvent.ACTION_UP:
ricerView.goDown();
}
}
else{

float touchY = event.getY();
int pointerIndex = event.getActionIndex();
int pointerID = event.getPointerId(pointerIndex);

switch (event.getActionMasked()) {
// When finger touches the screen, ricer will accelerate up
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
if( touchOnTopOfScreen(touchY) ){
pointerId = pointerID;
ricerView.goUp();
log("RV1 down Id=" + pointerID + " Y-pos=" + touchY);
}
else{
pointerId2 = pointerID;
ricerView2.goUp();
log("RV2 down Id=" + pointerID + " Y-pos=" + touchY);
}
break;

// When finger is lifted from the screen, ricer will accelerate down
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
if( pointerId == pointerID ){
ricerView.goDown();
pointerId = -1;
log("RV1 up Id=" + pointerID + " Y-pos=" + touchY);
}
if( pointerId2 == pointerID ){
ricerView2.goDown();
pointerId2 = -1;
log("RV2 up Id=" + pointerID + " Y-pos=" + touchY);
}
}
}

// Delegate the touch to the gestureDetector
mGestureDetector.onTouchEvent(event);

return true;

} // End of onTouchEvent

private boolean touchOnTopOfScreen(float y){
if(mDisplayHeight/2 > y)
return true;
else
return false;
}

最佳答案

问题出在你处理案件的方式上。

    case MotionEvent.ACTION_DOWN: 
case MotionEvent.ACTION_POINTER_DOWN:
if( touchOnTopOfScreen(touchY) ){
pointerId = pointerID;
ricerView.goUp();
log("RV1 down Id=" + pointerID + " Y-pos=" + touchY);
}
else{//<---should be a separate if-statement
pointerId2 = pointerID;
ricerView2.goUp();
log("RV2 down Id=" + pointerID + " Y-pos=" + touchY);
}
break;

在上面,有一个用于进入屏幕的额外指针和第一个指针的操作,但它们位于单个 if 语句 中。这意味着如果一种情况发生,另一种情况就不会发生。您应该将两个玩家的 Action 分成单独的 if 语句

关于java - Android 多点触控 : Not detecting MotionEvent. ACTION_POINTER_DOWN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22487024/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com