views:

52

answers:

2

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.

A: 

A Timer object would probably listen to the ENTER_FRAME event anyway for basing its calculations. Since it is a new object it would also use up a bit of memory and possibly more depending on the inner workings. This would only really be a problem if you were creating a lot of instances of the Timer class.

Assuming this is some dynamic world you are creating, you most likely already have a main game loop that is called on an ENTER_FRAME event. It is in here you would loop through all your tiles and update the text accordingly. You might need to store some timestamps or something if you need to record how long a tile has done something.. hard to say really without more of a description of what is happening :)

Allan
Timer does not internally listen to frame events. Like other events, timer events can fire independently of frame events.
fenomas
@fenomas I don't suppose you know what the Timer hooks into?
Allan
Allan, it doesn't hook into anything, or at least not anything we can hook into. Timer events come directly from the Flash player, just like mouse events and the like, and they get issued independently of frame updates. It's easy to verify - just check out how Timer events behave in a movie whose frame rate is 1FPS, and you'll see you can get several timer events between frame events.
fenomas
yes that makes sense. I was just curious to see if there was a way/hack to make use (thinking game loops) of whatever was updating the timer. I had a little look into the player code and indeed there is no event listeners. It seems something at the heart of the flash player somehow knows to call and update it.
Allan
+3  A: 

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.

fenomas