简体   繁体   中英

Why is my onTouchEvent being triggered without touching the screen?

The onTouchEvent method always says that the event is 0 (MotionEvent.ACTION_DOWN) once I touch the screen for the first time. I don't know how to get it to report when I stop touching the screen.

The stage variable tells it what tap number it is on in the three part process (start, stop, and release)(made for a catapult like game)

public static int stage = 0;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                stage++;
                System.out.println(stage);
                handleTouches();
                System.out.println(onTouchEvent(event));
                return onTouchEvent(event);
        }
        return true;
    }
    public void handleTouches() {
        if (stage == 1) {
            runPowerBar = true;

        } else if (stage == 2) {
            runPowerBar = false;
            tree.startAnimation();
            bg.startFlight(power);
        }
    }

    public void update() {
        tree.update();
        setCoins(context, 0);
        //setCoins(context, coins);

        if (runPowerBar) {
            powerBar.powerBarSlider();
        }
        System.out.println(runPowerBar);
    }

The update method is called by the method below in the MainThread

 @Override
    public void run() {
        while (running) {
            canvas = null;

            try {
                canvas = this.surfaceHolder.lockCanvas();
                synchronized(surfaceHolder) {
                    this.gameView.update();
                    this.gameView.draw(canvas);
                }
            } catch (Exception e) {} finally {
                if (canvas != null) {
                    try {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

I want the onTouchEvent to tell me that the action is down when i touch and up when I'm not but it just stays on 0 for down.

Update 7/22:

Alright so after some testing I found out that the update method still runs, but my draw method freezes once I tap the first time and it resumes after the second tap. Also it now says when I release a tap.

It seems like you have a recursive function there, you call onTouchEvent with the ACTION_DOWN event recursively.

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //Start of touch events 
                stage++;
                System.out.println(stage);
                handleTouches();
                System.out.println(onTouchEvent(event));
                //return onTouchEvent(event);
                return true;
            case MotionEvent.ACTION_UP:
                // End of touch events
                return true;
            case MotionEvent.ACTION_MOVE:
                // Moved touch events
                return true;
        }
        return true;
    }

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