Optimizing Flash Buttons (Part 2)

Last week I shared the method I use to optimize (movieclip) buttons within Flash. Now, I’m going to take it one step further, by putting the following on the first frame of the topmost layer:

_global.path = "/index.php";
_global.pg_1 = _global.path + "?id=1";

function eventListener(my_mc, mcInstance){
my_mc.onRollOver = function(){
this.gotoAndPlay("over");
}
my_mc.onRollOut = function(){
this.gotoAndPlay("out");
}
my_mc.onRelease = function(){
getPage(mcInstance);
}
}

function getPage(mcInstance){
var newPage = _global["pg_" + mcInstance];
getURL(newPage);
}

Then, on each movieclip I would place:

onClipEvent(load){
var instance = 1; // this number is different for every instance
_root.eventListener(this, _parent["_opt0" + instance + "_mc"], instance);
}

By creating a function and using it instead of having repetetive onRollover, onRollout movieclip method calls we’ve reduced the likelihood of errors from repetition and made future updates quicker and more efficient.