Optimize
In this paper, in order to talk about code optimization, it is not in-depth to OOP design level. Only involves some code-writing optimization principles mentioned in Flash8 help, and explains it.
The guidelines came from Flash8 help, and I did some explaining:
1. Avoid calling a function multiple times from one loop.
The contents of a small function are included in the loop, which makes the effect better. Small function has a short life span, which facilitates the release of resources. Especially in a big loop.
2. Use native functions whenever possible.
Native functions run faster than user-defined functions. Native functions are some of the functions in Flash (intrinsic), such as HitTest (), and you don't need to write a similar one yourself.
3. Do not use the Object type too much.
Data type annotations should be precise, which can improve performance. The Object type is used only if the appropriate alternative data type is not available. Also easy to code management, always know the type and role of the object.
It also facilitates compiler-compile-time optimization.
4. Avoid using the eval () function or the data access operator.
Generally, it is preferable and more efficient to set up a local reference only once. Use eval when you have to, such as converting _droptarget to MovieClip.
5. Assign array.length to variables, especially large loops, before starting the cycle.
Assign the Array.Length to a variable (such as Var ilength:number) before starting the loop, using it as a condition rather than using the myarr.length itself.
The reason, in the loop, Ilength is number variable, will be put into the register to use, more efficient than access to the array to get longer length. For example, you should use
var fontArr:Array = TextField.getFontList();
var arrayLen:Number = fontArr.length;
for (var i:Number = 0; i < arrayLen; i++) {
trace(fontArr[i]);
}
To replace:
var fontArr:Array = TextField.getFontList();
for (var i:Number = 0; i < fontArr.length; i++) {
trace(fontArr[i]);
}
6. Focus on optimizing the cycle and all repetitive actions.
Flash Player spends a lot of time dealing with loops (such as loops using the setinterval () function).
7. Do not use global variables when local variables are sufficient. Class static variables are also less used.
Global variables are a nightmare for developers. If you really need global variables, I recommend using the Singleton design pattern for management.
8. When declaring variables, add the var keyword.
This is to compile with the compiler to know your variable type and optimize the compilation.
Black Feather added two points:
1. be cautious about the use of keywords.
You are not in favor of using keywords as your own method and property name unless you are sure that subsequent development will not use the same event name and attribute name.
But how do you know how many hidden keywords flash uses? Too much! For example, ClassName, invalidate, refresh, mouseover and so on are not commonly used keywords. The good approach is to use the Sepy editor to write code that highlights all the published and not published keywords.
And because it is very possible and start,load, and so on these commonly used event names repeat, bring code unnecessary modification and trouble.
2. when it comes to functions that call drawing resources, try to make more judgments before calling them.
All gradients, position changes, create delete MC, component functions all involve drawing resource calls. In many cases, you can use logic to determine the properties of a variable or object, and then call those functions as necessary. This will save more computational resources.