AS2 Array of Multiple Buttons Sharing Same Actions As Well as Individual Actions

243 Views Asked by At

I have 5 button mcs that all share the same rollOver/rollOut function. However, they also each have a specific action on rollOver, for example mc1 would do the following on rollOver as well:

TweenLite.to(tips_mc.tip1, 1, { _alpha:100 });

How would I be able to add each individual call to each mc on rollOver in the array as well?

My array is as follows:

var btnArray:Array = new Array(mc1, mc2, mc3, mc4, mc5);
for (i=0; i<btnArray.length; i++) {
///initial state///
btnArray[i].enabled = true;
btnArray[i].id = i;
btnArray[i].onRollOver = function() {
    this.gotoAndPlay("over");
};
btnArray[i].onRollOut = function() {
    this.gotoAndPlay("out");
};

}

Any help very appreciated! Thanks!

1

There are 1 best solutions below

3
On BEST ANSWER

You can write your actions in different functions, and invoke them like that:

var functionsArray:Array /* of Function */ = [f1, f2, f3, f4, f5]; 
var btnArray:Array /* of MovieClip */ = [mc1, mc2, mc3, mc4, mc5];
var l:Number = btnArray.length;

for (var i:Number = 0; i < l; i++)
{
    btnArray[i].enabled = true;
    btnArray[i].id = i;
    btnArray[i].onRollOver = function()
    {
        functionsArray[this.id](); // you invoke your function here 
        this.gotoAndPlay("over");
    }
    btnArray[i].onRollOut = function()
    {
        this.gotoAndPlay("out");
    }
}

function f1():Void {trace("I'm the function 1");}
function f2():Void {trace("I'm the function 2");}
function f3():Void {trace("I'm the function 3");}
function f4():Void {trace("I'm the function 4");}
function f5():Void {trace("I'm the function 5");}