I
I
int02h2012-01-31 14:51:57
Android
int02h, 2012-01-31 14:51:57

What is the best way to implement a game loop in an Android game? Timer vs Handler?

Hello, dear Habra community! This topic has already been raised on Habré, but I could not find exactly what I want to know.
I am still new to programming for Android and for training and development I decided to write a simple game like "snake". Accordingly, the question arose: how to implement the game loop. I googled and found two acceptable (for a beginner) options. The first is through Timer + TimerTask, the second is through Handler + Runnable.
As a result, I have some doubts:
1. Timer. What happens if the frame does not have time to be drawn within the time period allocated to it? That is, the next tick of the timer will happen before the previous one has time to end. But drawing in my case blocks the canvas through synchronized, that is, the screen redrawing methods, as it were, “line up” on the blocking. What will happen in this case if the timer is stopped?
2. handler. As far as I understood, the next event is scheduled via postDelayed. But the rendering of the frame also takes some time. Thus, it turns out that fewer frames will be drawn per second than was intended. That is, you need to somehow calculate the time for scheduling the next event, and not use the constant 1000/FPS.
I understand that I asked too many questions, so I will be glad to receive at least some of the answers. Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Fushigi, 2012-09-01
@Fushigi

Do it so that it doesn’t slow down :) You climb into the jungle for the sake of a simple “snake”

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question