Android的多点触控事件的问题......而draging没有新的触摸事件?多点、事件、触控、问题

2023-09-06 18:31:54 作者:笑歎浮生若夢

那么,我尝试做一个小小的空间游戏,你控制使用触摸和拖​​动图形屏幕上的操纵杆船。我得到这个工作的罚款。问题出现,一旦我开始尝试添加触摸屏幕的顶部射击武器的能力。出于某种原因,如果你正在拖动操纵杆它似乎忽略了其他触摸输入,没有做任何事情(但是,一旦我停止拿着操纵杆,它工作正常)这是我无论我第一次使用Java,并与Android所以它可能是一些愚蠢的,但四被粘贴了几天。总之,这里我的code。

So what im 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.

以下是从我

公共类面板扩展SurfaceView实现SurfaceHolder.Callback {

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(事件);功能

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;
}

所以是,问题是,如果操纵杆的情况下的中间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. Any ideas?

推荐答案

您需要处理检测其他手指。更具体办理

You need to handle detecting the other finger. More specifically handling

MotionEvent.ACTION_POINTER_DOWN MotionEvent.ACTION_POINTER_UP

否则你忽略了其他手指。下面是我的项目已经做了你要寻找什么的一个约code:

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;
    }
}

请参阅 和here更多的解释。