Android Multi-touch event problems…No new touch events while dragging?

老子叫甜甜 提交于 2019-12-08 04:22:09

问题


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.

The below is from my

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
    }

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

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

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.


回答1:


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

MotionEvent.ACTION_POINTER_DOWN and 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.



来源:https://stackoverflow.com/questions/7034660/android-multi-touch-event-problems-no-new-touch-events-while-dragging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!