I'm doing a game with game loop and active rendering (using SurfaceView and SurfaceHolder.Callback).
I'm wondering how to make that work together with timed events and touch event management. The timed events are like each xx ms something happens, each yy ms something different happens. Touch management is update game and screen when down, up, and move.
I have made a few tries, but it looks somehow awkward and the game isnt performing very well..
On one side I have a classic game loop (in separate thread): while running update, render, draw, sleep a bit
Now I didn't know well how to make this work with timer loops. I used handler approach - make classes which extend handler, do what they have to do in handleMessage, and call then themselves with sendMessageDelayed.
They just update game state. The rendering of the resulting changes would be done in the gameLoop.
This approach needs synchronization for update of gameloop not intefering with update of the timers.
And - besides of this I need to handle touch events - down, up, and move! My approach here was, like in the timers, just update game state, and leave it to the loop to handle and display correctly.
I don't know if I'm maybe on the completly wrong path, noob to Androd, and made it just like i thought "could work".
For example I'm not sure if its necessary to make a game loop in a separate thread. I thought it was, to show the results of touch handling (move) according to certain framerate.
On the other side I could leave the loop and just tell to draw after each 开发者_如何学运维timer update, and after each touch handling, but this doesnt work well either.
The question is what's the general approach for this kind of game? (timed loops / touch event (move) which has to render to screen continuosly).
Thanks in advance.
Have you read Using Input Pipelines In Your Android Game? It presents a solution to your problem for handling input.
精彩评论