开发者

About code optimization in Flash AS3

开发者 https://www.devze.com 2023-01-17 10:52 出处:网络
i\'m curious about something. Let say we have 30 object in the code, and every object should be animated trough enter frame event. Which will be faster(in terms of memory execution) : to have just one

i'm curious about something. Let say we have 30 object in the code, and every object should be animated trough enter frame event. Which will be faster(in terms of memory execution) : to have just one event listener in the main object (for example) and to call object methods, or, to have separate event listener for every object? Thanks :) .

UPDATE: Well, actually, i need to monitor each object to find out when that object is on current frame, e.g. on end of animation, and than remove it from disp开发者_StackOverflow社区lay list. What is the best way to that?


For 30 objects, this doesn't really matter. Actually, in general it matters only little, since the bottleneck will always be rendering.
But to answer your question: Listeners are slow. For one notification to happen, an Event object needs to be created (allocation is generally quite expensive), and an untyped call of an anonymous function or a method closure needs to be done (which is signifficantly slower than calling a method on a strictly typed value).


You should try to use tweens instead of animating via ENTER FRAME event.

Animating on ENTER FRAME will cause tremendous lag, especially if you have lots of objects.

Checkout TweenLite: http://www.greensock.com/tweenlite/


Definitely to have just one event listener. The less listeners, the faster.


As a rule of thumb, one listener is better, and will induce a more predictible behaviour.

That being said, it depends pretty heavily on what you're trying to do. You might not have to animate every object on every enter frame event, thus gaining performance further.

0

精彩评论

暂无评论...
验证码 换一张
取 消