简体   繁体   中英

OnTouchEvent: I want to recognize touches while moving with an another finger on the screen

I'm trying to develop my own game. But I have an issue with the onTouchEvent-Method. Little explanation before showing my code. I want to be able to control a flight with my finger. It Should always follow my finger. For that I implemented the MotionEvent.ACTION_MOVE case. The flight only can move on the left side of the screen. When I touch on the right side of the screen, it should shoot a bullet. And it should be possible to fly around and shoot simultaneously. Here is my Code:

public boolean onTouchEvent(MotionEvent event) {

    int maskedaction= event.getActionMasked();


        switch (maskedaction) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:
                    if (event.getX() > screenX / 2 && flight.toShoot == 0) {
                        flight.toShoot++;
                    }
                break;

            case MotionEvent.ACTION_MOVE: {
                    if (event.getX() < screenX / 2 - 100) {
                        flight.x = (int) event.getX() - 100;
                        flight.y = (int) event.getY() - 50;
                    }
                    break;

            }
        }

    return true;
}

So that's the code. When I touch the left half of the screen, I can fly around with the plane. When I Touch the right side, I can shoot. But I cannot do it at the same time. I want to touch first the left half and fly around. By clicking on the right side, at the same time as i'm moving my finger on the left side, it should shoot a bullet.

I found out that when I click at the same time on both sides with one finger, then the plane shoots one bullet. If i release now the right finger(the finger that shoots when it touches the screen) I can fly around and while I'm flying around I can shoot simultaneously.

You can detect your touch by detecting IDs, https://developer.android.com/training/gestures/multi

You can do something like this :

    int leftSideId = 0;
    int rightSideId = 1;

    public boolean onTouchEvent(MotionEvent event) {

        int maskedaction= event.getActionMasked();

        // Get the pointer ID
        int mActivePointerId = event.getPointerId(0);

        switch (maskedaction) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:

                if (event.getX() > screenX / 2) {
                    rightSideId = mActivePointerId;
                    if(flight.toShoot == 0){
                        flight.toShoot++;
                    }
                }else{
                    leftSideId = mActivePointerId;

                }
                break;

            case MotionEvent.ACTION_MOVE: {
                if (event.getX() < screenX / 2 - 100) {

                    if (mActivePointerId == leftSideId) {

                        flight.x = (int) event.getX() - 100;
                        flight.y = (int) event.getY() - 50;
                    }
                }
                break;

            }
        }

        return true;
    }

So I found a solution by myself.

public boolean onTouchEvent(MotionEvent event){

    int amount=event.getPointerCount();
    for (int i=0; i<amount; i++) {
            float x = event.getX(i);
            float y = event.getY(i);


        if (x>screenX/2 && flight.toShoot==0){
            flight.toShoot++;
        }

        if (x<screenX/2 -100 && flyingenabled)
        {
            flight.x = (int) x - 100;
            flight.y = (int) y - 50;
        }



    }
    return true;
} 

Thats working for my problem. Easier than I thought.

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