gpt4 book ai didi

java - 如何处理多点触控?

转载 作者:行者123 更新时间:2023-11-30 02:29:11 27 4
gpt4 key购买 nike

我正在为 Android 制作一个平台游戏,我需要一些触摸事件方面的帮助。这是我的代码:

    public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
for (int i = 0; i < object.size(); i++) {
tempObject = object.get(i);
if (tempObject.getId() == ObjectId.Player) {

if (e.getAction() == MotionEvent.ACTION_MOVE) {

if (moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(true);
tempObject.setMovingRight(false);

}
if (moveLeftExit.contains(scaledX, scaledY)
&& !moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(false);

}
if (moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(true);
tempObject.setMovingLeft(false);

}
if (moveRightExit.contains(scaledX, scaledY)
&& !moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(false);

}

}
if (e.getAction() == MotionEvent.ACTION_UP
|| e.getAction() == MotionEvent.ACTION_OUTSIDE) {

if (moveLeft.contains(scaledX, scaledY)) {
tempObject.setMovingLeft(false);

}
if (moveRight.contains(scaledX, scaledY)) {
tempObject.setMovingRight(false);

}
}

if (e.getAction() == MotionEvent.ACTION_DOWN) {

if (jump.contains(scaledX, scaledY)) {
if(tempObject.getVelY() ==0)
tempObject.setVelY(-15);
}
}

}
}
return true;
}

当我使用一根手指时,一切都很好,如果我触摸 moveRight 矩形,角色会向右移动,当我将手指移开时,他会按预期停止。问题是,如果我在触摸其他按钮的同时触摸一个按钮,它不会对此使用react。所以我想我的问题是,如何修改我的代码以使其对多点触控使用react?

谢谢!! :)

最佳答案

这是一个简单的代码。

public boolean onTouch(View v, MotionEvent event) {

int action = MotionEventCompat.getActionMasked(event);
int pointerIndex = MotionEventCompat.getActionIndex(event);
int x = (int)MotionEventCompat.getX(event,pointerIndex);
int y = (int)MotionEventCompat.getY(event,pointerIndex);

switch(action)
{
case MotionEvent.ACTION_DOWN:
//
// First finger was touched. Set "pointerIndex" to some value (lets say 0 or -1)
// Save "pointerIndex" corresponding to this touched object.
//
break;
case MotionEvent.ACTION_POINTER_DOWN:
//
// More finger touched when first finger is already touched.
// Save "pointerIndex" corresponding to this touched object.
//
break;
case MotionEvent.ACTION_MOVE:
//
// Finger with "pointerIndex" was moved.
//
break;
case MotionEvent.ACTION_UP:
//
// The first touched finger went off.
//
break;
case MotionEvent.ACTION_POINTER_UP:
//
// Finger with "pointerIndex" went off (other than first finger)
//
break;
}
return true;
}

祝你好运。 :)

关于java - 如何处理多点触控?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27506882/

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