How to handle many display objects that need 'enterFrame' events

I use Composer and have many DisplayObjects in my scene. A lot of them require updates each frame (e.g. moving them around or check for collisions), so they have a function registered as an enterFrame event handler on the Runtime object.

The tricky part now is to manage all of these event handlers. In my scene’s hide event, I need to remove all of them, that means iterating through all my DisplayObjects. Also, each DisplayObject needs a finalize event handler to remove the enterFrame listener.

I find all of that very cumbersome, isn’t there an easier way to perform periodic updates on DisplayObjects?

If the constructor of each object attaches a Runtime listener for the enterFrame event and the finalize() removes that listener, you have no other work to do. Each object handles it’s own enterFrame event and when that object is removed it’s attachment to Runtime gets removed.

What else are you doing which is computationally expensive?

But those handlers would still be called after the scene has hidden, correct? That is what I am trying to achieve, remove them when the scene hides, attach them again when the scene comes to display.

Do you pass a reference to the scene object into the constructor of each object in the scene? If so, just have another listener for the show and hide events which add and remove the Runtime listener, rather than doing it in the finalize listener.

Yeah that would work. I was hoping there was a more elegant solution. If enterFrame events would be posted to every DisplayObject in the scene, that’d be fantastic!

For now I ended up having one Runtime enterFrame handler that traverses the display tree and calls an enterFrame function on every DisplayObject in the tree. While it works, I think it is quite intensive work for Lua to call 60 functions per second for each DisplayObject I have, I’d rather see this happening on the native side.

If every object attaches to the enterFrame event then it will get fired on all of them. You do need to attach the listener though.

You could always put a quick test at the top of your enterFrame listener functions:

if paused then return; end

and set a scene wide flag named “pause” that can be set to true or false. If you set it to true in your scene:hide(), your event handler’s will stop doing work and when the scene id destroyed they will be removed.

Rob

If the constructor of each object attaches a Runtime listener for the enterFrame event and the finalize() removes that listener, you have no other work to do. Each object handles it’s own enterFrame event and when that object is removed it’s attachment to Runtime gets removed.

What else are you doing which is computationally expensive?

But those handlers would still be called after the scene has hidden, correct? That is what I am trying to achieve, remove them when the scene hides, attach them again when the scene comes to display.

Do you pass a reference to the scene object into the constructor of each object in the scene? If so, just have another listener for the show and hide events which add and remove the Runtime listener, rather than doing it in the finalize listener.

Yeah that would work. I was hoping there was a more elegant solution. If enterFrame events would be posted to every DisplayObject in the scene, that’d be fantastic!

For now I ended up having one Runtime enterFrame handler that traverses the display tree and calls an enterFrame function on every DisplayObject in the tree. While it works, I think it is quite intensive work for Lua to call 60 functions per second for each DisplayObject I have, I’d rather see this happening on the native side.

If every object attaches to the enterFrame event then it will get fired on all of them. You do need to attach the listener though.

You could always put a quick test at the top of your enterFrame listener functions:

if paused then return; end

and set a scene wide flag named “pause” that can be set to true or false. If you set it to true in your scene:hide(), your event handler’s will stop doing work and when the scene id destroyed they will be removed.

Rob