Optimizing Flash and ActionScript
Please NOTE: all code samples are pseudo-code. You must correct their syntax.
Also NOTE: This is with Actionscript 3 in mind.
- Avoid Statics
- Flash takes much longer to call a static method from a different class than a instance method. Compare flash static calls.
- Reduce new sounds per frame
- Starting up a sound is CPU intensive. If you have a "bang" sound for each collision and multiple things collide in one frame, then launching multiple "bang" sounds will cause a delay. Reduce same sound calls to one per frame.
- Reduce work per frame
-
There are different checks you may need to do as your app runs. Often
you don't need to do the checks for every frame. Try breaking up common
checks between frames using the mod% operator:
switch(whichFrame%3) { case 0: CheckAccomplishments(); break; case 1: CheckMinuteInterval(): break; case 2: CheckHighScores(); break; } - Set a higher frame rate
- Flash will only drop one frame if your code is too slow. Instead of dropping a second frame, it draws what it had before. Visually, this is a vast improvement. Try setting frame rate to 50 to see if there is an improvement. Then tinker.
- Create local variables as pointers to more global variables
-
Variables to objects like "whichAnimal=dogObject" are like pointers to memory.
The Flash runtime needs to follow the pointer. Therefore:
whichAnimal=dogObject is faster than whichAnimal=Main.Set.Animals.dogObject
So try setting a local variable directly to the final value you need:public function Choose() { var whichAnimal = Main.Set.Animals.dog; for (i in 0...1000000) Use(whichAnimal); // faster for (i in 0...1000000) Use(Main.Set.Animals.dog); // slower }
Did you find this article userful for your website?
If so, why not provide a link to it? Just copy the following to your web page:
If so, why not provide a link to it? Just copy the following to your web page:
<a href='http://upwithabang.com/articles/flash-optimizing.html'>
Actionscript Optimizations
</a>
Share this:
