简体   繁体   中英

Android onTouchEvent() continuous touch not detected

I'm trying to implement a Joystick in an Camera centered player game. The joystick increments or not a "dx" or "dy". I created a GameControls class that handles all touches form the GameSurfaceView. Everything works well when I touch the screen and I'm moving the finger, but when I stop, the onTouch event stops getting fired.

public void handleMultitouch(MotionEvent event)
{
    int ptrId=-1;
    int action = event.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        point.x=(int) event.getX();
        point.y=(int) event.getY();
        if(joystickEntidade.colide(point))
        {
            c++;
            Log.e("down",event.getPointerId(0) +"");
            idJoystick=action;
            JSdown(point.x,point.y);
        }
        break;

    case MotionEvent.ACTION_UP:
        point.x=(int) event.getX();
        point.y=(int) event.getY();
        if(event.point(0)==point)
        {
            c=0;
        JSup(event);
        idJoystick=-1;
        }
        else
            Sup(point.x,point.y);
        break;  

    case MotionEvent.ACTION_POINTER_DOWN:
        ptrId = action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;


        int ptrIdx = event.findPointerIndex(ptrId);

        point.x=(int) event.getX(ptrIdx);
        point.y=(int) event.getY(ptrIdx);
        if (joystickEntidade.colide(point))
        {
            idJoystick=ptrId;
            JSdown(point.x,point.y);
        }
          else
                Sup( point.x,point.y);

    break;

    case MotionEvent.ACTION_POINTER_UP:
        ptrId = action >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        /*if (joystickEntidade.colide(pontoEntidade))
            JSup(event);
        else*/
        pontoEntidade.x=(int) event.getX(ptrId);
        pontoEntidade.y=(int) event.getY(ptrId);
        if(idJoystick==ptrId)
        {
            JSdown(pontoEntidade.x,pontoEntidade.y);
        }
        /* Log.e("APU", (int)event.getX(ptrId)+"-"+(int)event.getY(ptrId));*/
    break;


    case MotionEvent.ACTION_MOVE:
        point.x=(int) event.getX();
        point.y=(int) event.getY();
         for(int i = 0; i < event.getPointerCount(); ++i)
        if(event.getPointerId(i)==idJoystick)
        {
            c--;
        JSdown(point.x,point.y);
        }
        break;
    default:
        break;
    }

Is there any way to keep tracking the finger position, even if I'm not moving my finger?

Remember the last position. The onTouchListener only gets called when the finger position changes, not constantly. So if its still, no calls will be made. If its not touching, no calls will be made. That's how Android works, you need to work in that manner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM