Android多点触摸事件问题...拖动时没有新的触摸事件?

发布于 2024-11-29 08:06:56 字数 2730 浏览 1 评论 0 原文

所以我想做的是一个小太空游戏,你使用触摸拖动通过屏幕上的图形操纵杆来控制飞船。我已经让它工作得很好。当我开始尝试添加触摸屏幕顶部来发射武器的功能时,问题就出现了。由于某种原因,如果您当前拖动操纵杆,它似乎会忽略其他触摸输入并且不执行任何操作(但一旦我停止握住操纵杆,它就可以正常工作)。 这是我第一次使用 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 的中间,它似乎会阻止任何其他触摸事件的正确注册。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2024-12-06 08:06:56

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

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

请参阅 此处了解更多说明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文