views:

258

answers:

4

I am writing a game in AS3 and have, as an example, around 40 objects on screen. Let's say they are clouds. I'm wondering which of the two paths would be less a strain on system resources:

a) Put an eventListener on each object and have it update itself, or

b) Loop through the array and manually update each object

Or is the performance difference negligable? Does either solution scale better than the others?

+1  A: 

I would expect the performance to be fairly negligable either way. Once you get a lot of objects you might see a difference (with the loop being the winner). From my experience Adobe put a lot of work into optimizing the actionscript event listener path.

Aaron
A: 

I am under the impression that event listeners require more resources so it is better to use an array when performance is required. It would also scale better for that reason. If you are using Flash 10 then use a Vector, I believe it offers better performance than an array (and type saftey).

Allan
Type "safety" or "limitation"?
TandemAdam
The only limitation is when you need to create a generic data structure and thats only because AS3 does not support generics :(
Allan
A: 

Use EventListeners! Just make sure you manage them properly ie. remove them when you done, use weak references.

You wont really find much performance from doing things like this. Usually better performance comes from the bigger ticked items, like not using filters, lowering the frame-rate etc. So punishing your code clarity and OO for the sake of a half a millisecond is not worth it in my book.

There are a few really great guides out there that will teach you all about optimizing in AS3. The best one I have found is Grant Skinner's; AS3 Resource Management. And I just found a quicker seven step version. I would definitely recommend everyone doing AS3 should read the Grant Skinner slides though.

Of course don't just take my word (or anyone else answering your question), you can do you own tests and actually see whats up using a profiler. See Lee Brimlow's latest video tutorial for how to do this. It is worth the watch! Check it out here: GotoAndLearn - ActionScript 3 Performance Testing.

TandemAdam
I would not say that by not using Event Listeners that you will be sacrificing clarity and OO. The way I program games is quite different to normal interactive items. I tend to have a game loop and in that I update the world, which will loop through the entities and update them (and perhaps using quad trees for optimisation). This way seems to be the De Facto standard for game design from my observations (XNA framework, some C++ codes samples).
Allan
What you say it true, except I would generally conform to the best practices of the language. In AS3 I usually try to follow how Adobe does it.
TandemAdam
A: 

This is a tricky one, purists would say to to go with the array looping method, as with this method it would be possible to cut out the 'view' out of the MVC and still have the system working (which is a good test of any system). However, if you are working with the events you can cut some corners with event bubbling and strict typing. For example if we assume you make a custom event called CloudEvent that has a property called cloud that contains a reference to the dispatching CloudSprite, then as long as the event bubbles by default, you don't need to add an event listener to each one, just to the DisplayObjectContainer which holds them (which I am imaginatively calling CloudContainer). This way the event bubbles up and you only have to add one listener, and don't have to worry about managing listeners on child items.

public function CloudContainer()
{
 super();
 addEventListener(CloudEvent.CHANGE, cloudChangeHandler);
}

private function cloudChangeHandler(evt:CloudEvent):void
{
 var cloud:CloudSprite = evt.cloud;
 cloud.update();
}

Hope this helps