开发者

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

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜