
Originally Posted by
5TonsOfFlax
Or you could create a function which creates specific functions.
Code:
function makeNavigate(myLabel:String):Function{
return function(e:Event):void{
navigate(myLabel); //navigate is defined elsewhere
}
}
button1.addEventListener(MouseEvent.CLICK, makeNavigate("FrameLabel1"), false, 0, true);
So, using your example... why does this work:
Code:
Button1.addEventListener(MouseEvent.CLICK, makeNavigate("FrameLabel1"), false, 0, true);
Button2.addEventListener(MouseEvent.CLICK, makeNavigate("FrameLabel2"), false, 0, true);
function makeNavigate(myLabel:String):Function{
return function(evt:MouseEvent):void{
gotoAndStop(myLabel);
}
}
But this returns a 1067 compiler error:
Code:
Button1.addEventListener(MouseEvent.CLICK, makeNavigate("FrameLabel1"), false, 0, true);
Button2.addEventListener(MouseEvent.CLICK, makeNavigate("FrameLabel2"), false, 0, true);
function makeNavigate(myLabel:String):void{
gotoAndStop(myLabel);
}
Why do I need to "bury" the gotoAndStop() action in another function? Why does the makeNavigate function need to return a Function data type instead of just being "void" and executing the gotoAndStop() action itself?