I'm looking for an optimized way to handle simple state transitions on my game objects. By "simple" I mean not more than 1-2 actions per object, and most algorithms are linear.
For example, I have an UI element that implements a switch (like a light switch) and it has two states - "up" (lights turned on) and "down" (lights turned off). Let's say I want to make it so that I can switch the switch from one state to another, and update its presentation in a smooth way using linear interpolation (move the foreground sprite up or down).
The question is if I should always check if the object should update its state (every frame), or should I add/remove this object from the update loop on demand, or is there any other (better) way of handling this situation?
Currently I have it implemented the following way: my object has a boolean variable called
animated and in my
onframe loop I have the following code:
if (this.animated) /* calculate new positions for the sprite */ else /* do nothing */`
And it would appear to me that this kind of code is not very well optimized. How to handle this kind of things better?