So I’ve about hundred of map tiles and each title has some object which does some stuff. And one of things it should do is to update text (a time after which you need to do something) in text field. How should i do it better – with enter frame event and on each enter frame update it or set up a timer object which on each second would update the text in text field? I wonder more from performance view which would be better.
Share
You should use frame events any time you want to do processing that ultimately affects what shows up on the screen. If you’re moving images around, or resizing visual contents, or doing programmatic animations, or anything of that nature, you should use frame events. The reasoning why can get fairly involved, but at the simplest level it boils down to this: if you do that kind of processing more than once between screen updates, you’re wasting performance for no reason, and if you do it less often you get choppy visuals. Using a Timer to do things at the same speed as the frame rate might seem like an alternative, but you’ll spend a certain amount of time out of sync. That is, even if your content is 25FPS and you run a timer with a delay of 40ms, because of small fluctuations in timing, sometimes that timer event will fire twice between redraws, and sometimes it will get skipped. That might be worth it if there was some huge inherent advantage to using Timer, but there isn’t.
On the other hand, any time you want to do something that happens a lot less often than screen updates, Timer is a good option. It’s clearer to make a timer event that fires in 5 seconds than it is to make a frame listener that counts up to 200. (But be wary of mixing frame events and timers together – see here.)
Finally a word about performance: it doesn’t make any difference which you use. Or rather, whatever difference there is will be tiny compared to rendering, and what you do inside the frame or timer events. What can make a slight difference is minimizing the overhead – if you do a lot of different things every frame, use a single event listener, and have it call all the functions that need to fire, rather than using lots of listeners. And the same thing goes for Timers. But even this, realistically is unlikely to dent your performance unless you’re running many hundreds of listeners. You should always start out whatever way is simplest, and worry about performance later if testing shows you have a bottleneck.