I got a question about good practice. I’m making a simple game and, I’m messing around with some sprites.
I have a sprite class. In that class there is a timer that runs every few milliseconds.
Now, I have a List<Creatures> and everyone of the “Creatures”, got a Sprite object in them to run the animation. So that mean every creature has a timer that does the same thing.
I decided to change that, and make one timer that just iterate on the list, and call the function that create the animation for each “Creature”.
What is a good practice?
- Give every “Creature” its own timer.
- Creating one “big” timer in the Form that iterate on the list, and call the function that is creating the animation from each object.
- A different way
Unfortunately, it doesn’t in reality. You are limited by the resolution of the (non-high performance) system clock, which is in the neighborhood of ~15 ms. So, you can set an interval of 1 if you like, but that’s not what you actually get.
Back on topic, for a game, everything should run on the same clock. For each tick, you will have a loop which updates the logical state of the game (positions and whatnot) and then the display (animation frames, blit to screen, etc.). You will likely run into problems if you have them on different timers as it will become extremely difficult to coordinate the state of your objects when they are being updated in parallel.
Also realize that you will need to normalize the update rate to ensure that things don’t update too quickly or too slowly (depending on how long it takes for you to render a frame). You need to check the delta between the last update and the new update and act accordingly.