简体   繁体   中英

Continuous touch detection in Android

I am developing an android game. I have the following code to check if my finger is on the screen

if(e.getAction() == MotionEvent.ACTION_UP) this.downPressed=false; else this.downPressed=true;

if(this.downPressed) { //move the object }

I checked using Log.e("Motion",String.valueOf(e.getAction())); When I initially place my finger on the screen, ACTION_DOWN is detected.

Then when I still have my finger on the screen, few traces of ACTION_MOVE is detected

Then when I still have my finger on the screen, nothing is detected

Finally when I take my finger off the screen, ACTION_UP is detected

I am not able to successfully detect anything when my finger is laid still on the screen. Please help me in detecting this continuous touch.

You should not be doing the move logic inside your onTouch event method. Rather, like you are currently doing, set a flag when TOUCH_DOWN is detected and clear when TOUCH_UP is fired. Then make an update handler that continuously runs. If the flag is set it moves the object.

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