简体   繁体   English

Action_Move Android 问题

[英]Action_Move Android issue

I have been working on a game for a while, and I am working on the controls right now, and I am hitting an issue with the Action_Move.我一直在开发一款游戏,现在我正在开发控件,但我遇到了 Action_Move 的问题。 My code is set up as shown below.我的代码设置如下所示。

for(int i = 0; i < event.getPointerCount(); i++)
            {
                if(event.getPointerId(i) == 0 && primDown)
                {
                    x = (int) event.getX();
                    y = (int) event.getY();
                }
                else if(event.getPointerId(i) == 1 && secDown)
                {
                    x1 = (int) event.getX(1);
                    y1 = (int) event.getY(1);
                    if(dirPadRight.contains(x1, y1))
                    {
                        Log.w("game", "Right " + x1 +" " + y1);
                    }
                    if(dirPadLeft.contains(x1, y1))
                    {
                        Log.w("game", "Left " + x1 +" " + y1);
                    }
                }
            }

The primDown and secDown booleans are set up in the switch statement below: primDown 和 secDown 布尔值在下面的 switch 语句中设置:

int index = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT;
            int pointerId = event.getPointerId(index);

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                if(pointerId == 0)
                {
                    primDown = true;
                }
                else if(pointerId == 1)
                {
                    secDown = true;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                if(pointerId == 0)
                {
                    x = 0;
                    y = 0;
                    primDown = false;
                }
                else if(pointerId == 1)
                {
                    x1 = 0;
                    y1 = 0;
                    secDown = false;
                }                      
                break;
            }

Basically, I am just having it refresh the x, y, x1, and y1 any time any action happens, so that I don't have to deal with ACTION_MOVE in the switch statement, but when I try moving with the secondary pointer, it doesn't change the event.getX(1) or event.getY(1).基本上,我只是让它在任何动作发生时刷新 x、y、x1 和 y1,这样我就不必在 switch 语句中处理 ACTION_MOVE,但是当我尝试使用辅助指针移动时,它不会更改 event.getX(1) 或 event.getY(1)。 AS you can see, I put the log statements into the second pointer part of my for loop, and they go off whether I have both the primary pointer down and the secondary down, or if I had lifted the primary pointer and just the secondary is down, but it isn't changing the x1 or y1, it keeps outputting the original points.如您所见,我将日志语句放入我的 for 循环的第二个指针部分,它们 go 是否我已经将主指针和辅助指针都向下,或者如果我已经提升了主指针而只有辅助指针是向下,但它并没有改变 x1 或 y1,它一直在输出原始点。

Does anyone have any idea why this isn't working for me?有谁知道为什么这对我不起作用? It is weird that it knows a movement is happening, but it just isn't changing the x and y's, so I figured someone would know here.奇怪的是它知道运动正在发生,但它只是没有改变 x 和 y,所以我想有人会在这里知道。 Let me know if you need any more code.如果您需要更多代码,请告诉我。 Thanks in advance!提前致谢!

WWaldo沃尔多

try like this试试这样

if(pointerId == 0)
                {
                    primDown = true;
                    secDown=false;
                }
                else if(pointerId == 1)
                {
                    secDown = true;
                    primDown=false;
                }

This is difficult to say what causing your problem without seeing all the code.May be you are messing up with the boolean variable so that whenever a code should work it is not working.在没有看到所有代码的情况下很难说是什么导致了您的问题。可能是您弄乱了 boolean 变量,因此每当代码应该工作时,它就无法工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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