AS3 CPU Performance with +100 MovieClip copies
I have a movieclip wich has a simple movement of a simple shape. This movieclip is copied many times to create a graphical effect.
As the number of copies, or the complexity of the movieclip increases, CPU usage goes up. As from 90 copies, when I keep moving the mouse over the swf movie in a circle, playback stops (hangs). When the mousemovement stops, the swf continues again at the right position, as if calculation has continued, but updating the display has not.
Now this is a quadcore pc, and we're 2010 ... I can't believe or accept that 1开发者_如何学运维00 mc's is the maximum number of movieclips that can be rendered ... There has to be a better way ...
The obvious question is what can be done to optimize CPU performance ?
I'm using AS3 , flash player 9. Movieclip is a simle rectangle shape which moves along 1 axis.
var myLinkage:Class = Class(getDefinitionByName(getQualifiedClassName(McToRepeat)));
var newMC:MovieClip = new myLinkage();
containerMC.addChild( newMC );
The swf probably hangs because when you move your mouse, you trigger MOUSE_MOVE
events and the like on every single MovieClip on the screen.
You can generally optimize the Flash rendering by going a few steps higher on the MovieClip
inheritance tree and building your own graphics class from there. MovieClip
s have a lot of cool bells and whistles, like timelines, layers, and built-in mouse handling. Unfortunately, all of those take up memory and CPU time, and pretty often, you won't need all of those features. Fortunately, you can pick and choose the features you need, and get rid of a lot of the excess fat that MovieClip
s have. The MovieClip
inheritance tree is laid out so each of those features is encapsulated pretty succinctly in one parent class.
For example, with your application, you say that your MovieClip
s contain a simple shape following a simple motion path. I'm also assuming that they're not going to interact with the mouse or keyboard. We can go up MovieClip
's inheritance tree pretty far because of that. Since you're only moving a shape, and not really animating parts of it, you can replace the timeline animation with a tween (I'd recommend looking at TweenLite by Greensock). Right there, you can write your own class that subclasses Sprite
(instead of MovieClip
), and probably get a decent increase in framerate.
If you want to optimize further, you can go further up. You also probably don't need layers, since you're using a simple shape. Therefore, you can go up further than DisplayObjectContainer
. Since you don't need interactivity, InteractiveObject
can also go. So you've trimmed a lot of fat off, and now you're at DisplayObject
. Since DisplayObject
s can be difficult to work with, you might want to go down the tree one step, either to Shape
or Bitmap
, depending on how complex your shape is.
Once again, when you decide how much you need from Actionscript's built in graphics classes, you may have to build your own from there. Since you're adding custom features to your display object, you will need to create your own class that inherits from either Sprite
, Shape
, or Bitmap
, and add that functionality there.
And to answer your question about why 100 MCs is slow, keep in mind that Flash runs on a virtual machine, and that layer of abstraction is a big resource hog.
精彩评论