Can I synchronize touch events and updates in an Android game?

So, the game works fine, but I noticed that in extremely rare cases the application crashes in random places. I did my debugging, and my theory is that since touch events on the surface are processed in the user interface thread of the application, and games are updated in a separate thread (the thread of the loop or what it is called), these two step on top of each other. My solution was to synchronize surface touch events:

@Override public boolean onTouchEvent(MotionEvent event) { synchronized (this.gameLoopingThread) { //Handle touch events here return true; } } 

with updating the game in the loop thread:

 @Override public void run() { while (this.running) { synchronized (this) { doUpdate(); } doDraw(); } } 

So I wonder if I am okay to do this? I am not very good at multi-threaded applications, so someone will tell me if this solves my problem, because I cannot check, because the problem is not often enough (this happened only twice). And if so, shouldn't we do this in every game we play? Textbooks that I have seen everywhere on the Internet never do this. Is there a reason?

+4
source share
1 answer

Synchronized helps block thread update methods for touch events, so I also implemented my game. I notice that it is stable when synchronized in both directions, and only one leaves it unstable.

0
source

All Articles