Reputation: 1973
Ok, so in creating a game in my spare time, I've been completely stuck when trying to code around the following scenario:
I want to capture tap events (TouchEvent.ACTION_DOWN
), and then in 500ms intervals, I want to check to see if the screen is still pressed, and repeat the same action, ad infinitum.
So, I started out with:
switch (myTouchHandler.getAction()) {
case (TouchEvent.ACTION_DOWN):
case (TouchEvent.ACTION_MOVE):
if (inputIntervalTooSoon()) return true;
//do the magic i intend to do....
}
....
private boolean inputIntervalTooSoon() {
final long now = System.currentTimeMillis();
if ((now - lastTouchEventTime) < 500) return false;
lastTouchEventTime = now;
return true;
And this produces the effect that It will wait the 500ms, but it doesn't begin to immediately detect if my finger is still down. It will pick it up again if I move my finger slightly, and then go back to dormant.
If I dont include the Interval function, my action just fires constantly. Any ideas in how to better implement this would be greatly appreciated.
Upvotes: 1
Views: 364
Reputation: 42849
I could imagine a Timing thread that is started on a TouchEvent.ACTION_DOWN, which would be started and run inside a loop that sleeps for 500ms. After the sleep it would then be able to perform its check, which would do your custom processing as well as checking whether or not TouchEvent.ACTION_UP happened or not (which you would have to record in the EventHandler).
Of course there is a bit of synchronization that would be required.
Does this make sense?
Something along the lines of this:
boolean upHappened;
switch (myTouchHandler.getAction()) {
case (TouchEvent.ACTION_DOWN):
upHappened = false;
new Thread(new TimerRunnable()).start();
case (TouchEvent.ACTION_UP):
case (TouchEvent.ACTION_CANCEL):
upHappened = true;
}
class TimerRunnable implements Runnable {
public void run() {
while(true) {
Thread.sleep(500);
if(upHappened) break;
//custom processing
}
}
}
Upvotes: 3
Reputation: 40391
The screen will be pressed until you have an event with either an ACTION_UP or ACTION_CANCEL actions, so I would check inversely.
Upvotes: 0