|
-
[RESOLVED] Is it possible to pass an argument to the function triggered by an event handler?
Hello All,
Trying to migrate my way of thinking from AS2 to CS4/AS3.
Ok, I have 2 buttons on the stage. Each button does almost the same thing, so I want to create a single function, and each button calls that same function (we'll name that function "Navigate")... however, the function will need to end up doing something different dependant on which button was clicked.
So, previously, in AS2, I would've added some code onto the buttons themselves with on(release) methods, like so:
Code:
// Define the Navigate function
function Navigate(myLabel){
gotoAndStop(myLabel);
}
// code on Button1
on(release){
Navigate("FrameLabel1");
}
// code on Button2
on(release){
Navigate("FrameLabel2");
}
So, each button effectively calls the Navigate function, and passes a different frame label to the function.
Now, I'm trying to recreate this functionality in AS3. As you all know, on(release) has been done away with (still don't know why), but we now have to use event handlers, so I'm trying to figure out a way to pass a different frame label argument to the Navigate function. Currently I can achieve that by using a switch statement to test which button was clicked, and act accordingly (see below).
Code:
// Add listeners to buttons
Button1.addEventListener(MouseEvent.CLICK, Navigate, false, 0, true);
Button2.addEventListener(MouseEvent.CLICK, Navigate, false, 0, true);
// Define the Navigate function
function Navigate(evt:MouseEvent):void{
switch(evt.target.name){
case "Button1":
gotoAndStop("FrameLabel1");
break;
case "Button2":
gotoAndStop("FrameLabel2");
break;
default:
gotoAndStop(1)
}
}
In this over-simplified example, this works fine, but in the real world I'm going to have more than 2 buttons, and the Navigate function would probably be much more complicated...
So, I would like to be able to pass an argument(s) (like in the AS2 example) to the Navigate function... perhaps in the addEventListener() method? I tried this, but got compiler errors:
Code:
// Add listeners to buttons
Button1.addEventListener(MouseEvent.CLICK, Navigate("FrameLabel1"), false, 0, true);
Button2.addEventListener(MouseEvent.CLICK, Navigate("FrameLabel2"), false, 0, true);
// Define the Navigate function
function Navigate(myLabel){
gotoAndStop(myLabel);
}
The Million Dollar Question:
Is it possible to dynamically pass/change an argument(s) to a function which is triggered by an event listener? (Or is the event that triggered the function the only argument you can have?)
If this isn't possible, I'd greatly like to hear how you folks would handle this (other than having a switch statement with 12 cases in it)???
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|