简体   繁体   中英

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. 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:

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). 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.

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. 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.

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