Avoid using onClipEvent() and on() events. Instead, use onEnterFrame, onPress, etc. There are several reasons for doing this:
The only problem exists when you need an onLoad function-style handler to be installed before the first frame is executed. In this case, you may use the undocumented event handler onClipEvent(construct) to install the onEnterFrame:
onClipEvent(construct) { this.onLoad = function() { //function body } }
Or, use onClipEvent(load) and call a regular function from it. This approach is less effective however, as there will be additional overhead for the extra function call.
Use the var keyword whenever possible. It is especially important to do so inside functions, as the AS compiler optimizes access to local variables by using internal registers with direct access by index rather than putting them into hash tables and accessing by names. Using the var keyword can double the AS function’s execution speed.
Un-optimized Code:
var i = 1000; countIt = function() { num = 0; for(j=0; j<i; j++) { j++; num += Math.random(); } displayNumber.text = num; }
Optimized Code:
var i = 1000; countIt = function() { var num = 0; var ii = i; for(var j=0; j<ii; j++) { j++; num += Math.random(); } displayNumber.text = num; }
Precache frequently accessed read-only object members in local variables (with var keyword).
Un-optimized Code:
function foo(var obj:Object) { for (var i = 0; i < obj.size; i++) { obj.value[i] = obj.num1 * obj.num2; } }
Optimized Code:
function foo(var obj:Object) { var sz = obj.size; var n1 = obj.num1; var n2 = obj.num1; for (var i = 0; i < sz; i++) { obj.value[i] = n1*n2; } }
Precaching can be used effectively for other scenarios as well. Some further examples include:
var floor = Math.floor var ceil = Math.ceil num = floor(x) – ceil(y); var keyDown = Key.isDown; var keyLeft = Key.LEFT; if (keyDown(keyLeft)) { // do something; }
Avoid repeating usage of long paths, such as:
mc.ch1.hc3.djf3.jd9._x = 233; mc.ch1.hc3.djf3._x = 455;
Precache parts of the file path in local variables:
var djf3 = mc.ch1.hc3.djf3; djf3._x = 455; var jd9 = djf3.jd9; jd9._x = 223;
Avoid complex, C-style expressions, such as:
this[_global.mynames[queue]][_global.slots[i]].gosplash.text = _global.MyStrings[queue];
Split this expression into smaller parts, storing intermediate data in local variables:
var _splqueue = this[_global.mynames[queue]]; var _splstring = _global.MyStrings[queue]; var slot_i = _global.slots[i]; _splqueue[slot_i].gosplash.text = _splstring;
This is especially important if there are multiple references on those split parts, for example in the following loop:
for(i=0; i<3; i++) { this[_global.mynames[queue]][_global.slots[i]].gosplash.text = _global.MyStrings[queue]; this[_global.mynames[queue]][_global.slots[i]].gosplash2.text = _global.MyStrings[queue]; this[_global.mynames[queue]][_global.slots[i]].gosplash2.textColor = 0x000000; }
An improved version of the previous loop would be as follows:
var _splqueue = this[_global.mynames[queue]]; var _splstring = _global.MyStrings[queue]; for (var i=0; i<3; i++) { var slot_i = _global.slots[i]; _splqueue[slot_i].gosplash.text = _splstring; _splqueue[slot_i].gosplash2.text = splstring; _splqueue[slot_i].gosplash2.textColor = 0x000000; }
The above code can be further optimized. Eliminate multiple references to the same element of array, if possible. Precache the resolved object in a local variable:
var _splqueue = this[_global.mynames[queue]]; var _splstring = _global.MyStrings[queue]; for (var i=0; i<3; i++) { var slot_i = _global.slots[i]; var elem = _splqueue[slot_i]; elem.gosplash.text = _splstring; var gspl2 = elem.gosplash2; gspl2.text = splstring; gspl2.textColor = 0x000000; }