1

所以我想做的是一个小太空游戏,你用一个图形屏幕操纵杆控制飞船,使用touchdrag。我已经让这个工作正常了。一旦我开始尝试添加触摸屏幕顶部以发射武器的能力,问题就出现了。出于某种原因,如果您当前正在拖动操纵杆,它似乎会忽略其他触摸输入并且不执行任何操作(但是一旦我停止按住 ,它就可以正常工作joystick)。这是我第一次使用java,Android所以它可能很愚蠢,但我已经坚持了几天。无论如何,这是我的代码。

以下来自我的

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

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(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它似乎会阻止任何其他触摸事件正确注册。

4

1 回答 1

5

您需要处理检测另一根手指。更具体地处理

MotionEvent.ACTION_POINTER_DOWNMotionEvent.ACTION_POINTER_UP

否则你会忽略其他手指。这是我的一个项目中的一些代码,它们完全符合您的要求:

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

有关更多说明,请参见此处此处

于 2011-08-12T01:33:31.790 回答