So we’re working on the android version of the iPhone game Snake Race (it’s a cool game). However, we seem to be getting a delay on the touch inputs just big enough to make a difference. (The snake might turn a step later than you intended) After getting almost used to the android version, the iPhone version feels like a blessing.
The question is whether there is a delay from androids side in calling the onTouchEvent() method? (I’m using Samsung Galaxy S2) If so, is there any neat tricks to circumvent the issue?(Even if it means using the NDK)
This is the current implementation touch handling:
public boolean onTouchEvent(final MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN ||
(action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN){
Run run = GameVars.run;
if(run != null){
if(run.paused){
run.paused = false;
}else{
if(event.getX(action >> MotionEvent.ACTION_POINTER_ID_SHIFT) < GameVars.width/2){
run.addTap(-1);
}else{
run.addTap(1);
}
}
}
return true;
}
return false;
}
This is the addTap method:
public void addTap(int newDir) {
if(newDir == -tapDir){
taps++;
tapDir *= -1;
}
}
And then every frame, taps is checked, and if it’s greater than zero (and we’re allowed to turn atm) we turn.
It all works fine and is reliable, but there is a consistent delay (maybe 2-3 frames, which is enough to make the iPhone snake seriously more maneuverable).
Any feedback is apprieciated!
I’ve come to realize that it is indeed a delay from the side of the device. It is not big but it is there, and it varies quite a lot between different android phones. My Galaxy S2 has a bigger delay than most of the newer HTC models I’ve tested. I’ve also made new project where a touch switch background / play a sound (I know that has an additional delay) etc. And I can’t seem to find anything that circumvents the problem.