Android多点触控事件问题...拖动时没有新的触控事件? [英] Android Multi-touch event problems...No new touch events while dragging?
问题描述
所以我想做的是一个小空间游戏,你使用图形屏幕操纵杆使用触摸
控制船只和拖动
。我已经让这个工作得很好。一旦我开始尝试添加触摸屏幕顶部以发射武器的能力,就会出现问题。出于某种原因,如果您当前拖动操纵杆,它似乎忽略了其他触摸输入并且没有做任何事情(但是一旦我停止按住操纵杆
)。
这是我第一次使用java,并且使用 Android
所以它可能是一些愚蠢的东西,但静止了几天。
无论如何,这是我的代码。
So what I'm trying to make is a little space game, You control the ship with a graphic on-screen joystick using touch
and drag
. I have gotten this to work fine. The problem arises once I started to try and add the ability to touch the top portion of the screen to fire a weapon. For some reason, if you are currently dragging the joystick it seems to ignore the other touch input and doesn't do anything (But it works fine once I stop holding the joystick
).
This is my both my first time working with java, and with Android
so its probably something stupid, but iv been stuck on it for a few days.
Anyway, here my code.
以下是我的
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
@Override
public boolean onTouchEvent(MotionEvent event) {
fingers= event.getPointerCount(); //Returns 1 or 2 properly if 2 fingers on screen
playership.onTouchEvent(event); //Pass the event along to the spaceship object
return true;
//return super.onTouchEvent(event); //no idea what this does, but it seems to disable the drag event
}
以下是playership.onTouchEvent(event);函数
Below is the playership.onTouchEvent(event); function
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
if(event.getY()<(Panel.mHeight-150))
{
Laser1.reset(mX,mY,(int)event.getX(),(int)event.getY());
}
break;
}
joy1.onTouchEvent(event);
return true;
}
以下是joy1.onTouchEvent(事件);
And below is the joy1.onTouchEvent(event);
public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();
//Offset between mouse(x,y) and center
double offx=oX-event.getX();
double offy=oY-event.getY();
double d=Math.sqrt((offx*offx)+(offy*offy));
switch (eventaction )
{
case MotionEvent.ACTION_DOWN:
if(d<dragrange) //Start Dragging if clicked
{
offx=offx/d;
offy=offy/d;
Dragging=true;
reset = false;
dX=(int) (offx*dragrange);
dY=(int) (offy*dragrange);
}
break;
case MotionEvent.ACTION_MOVE:
if(Dragging)//if joy is already being draged, update it.
{
offx=offx/d;
offy=offy/d;
if(d>dragrange)
{
dX=(int) (offx*dragrange);
dY=(int) (offy*dragrange);
}
else
{
dX=(int) (offx*d);
dY=(int) (offy*d);
}
reset = false;
}
break;
case MotionEvent.ACTION_UP:
if(Dragging)
{
Dragging=false;
}
break;
}
return true;
}
所以是的,问题是如果操纵杆在一个中间case MotionEvent.ACTION_MOVE
它似乎阻止任何其他触摸事件正确注册。
So yeah, the problem is that if the joystick is in the middle of a case MotionEvent.ACTION_MOVE
it seems to block any other touch events from properly registering.
推荐答案
您需要处理另一根手指的检测。更具体地说处理
You need to handle detecting the other finger. More specifically handling
MotionEvent.ACTION_POINTER_DOWN
和 MotionEvent.ACTION_POINTER_UP
否则你会忽略其他手指。以下是我的一个项目中的一些代码,它们正是您正在寻找的内容:
otherwise you're ignoring other fingers. Here is some code from one of my projects that does exactly what you're looking for:
public void onTouchEvent(MotionEvent event)
{
int ptrId = -1;
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
down(event.getPointerId(0), (int)event.getX(), (int)event.getY());
break;
case MotionEvent.ACTION_UP:
up(event.getPointerId(0));
break;
case MotionEvent.ACTION_POINTER_DOWN:
ptrId = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
int ptrIdx = event.findPointerIndex(ptrId);
down(ptrId, (int)event.getX(ptrIdx), (int)event.getY(ptrIdx));
break;
case MotionEvent.ACTION_POINTER_UP:
ptrId = action >> MotionEvent.ACTION_POINTER_ID_SHIFT;
up(ptrId);
break;
case MotionEvent.ACTION_MOVE:
for(int i = 0; i < event.getPointerCount(); ++i)
if(event.getPointerId(i) == inputPad.id())
{
inputPad.position(event.getX(inputPad.id()));
player.velocity(inputPad.delta());
player.stand();
if(enemy != null) {
Fighter.collide(player, enemy);
enemy.update();
}
player.update();
break;
}
break;
}
}
See here and here for more explanation.
这篇关于Android多点触控事件问题...拖动时没有新的触控事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!