Skip to main content
Inspiring
January 19, 2009
Question

Simple function parameter problem

  • January 19, 2009
  • 6 replies
  • 907 views
I am going to have about 73 buttons on a couple of screens of my flash module and I'd like to obviously be able to use the same function to run all of them, but I keep getting errors. Here is the code I'm using:

btn_1.addEventListener(MouseEvent.MOUSE_DOWN,goLink(1));
btn_2.addEventListener(MouseEvent.MOUSE_DOWN,goLink(2));
btn_3.addEventListener(MouseEvent.MOUSE_DOWN,goLink(3));

function goLink(num:Number) {
counter = num;
gotoAndStop(2);
}

I am trying to pass the number to the function so it will update the counter and load the right stuff on frame 2.

This topic has been closed for replies.

6 replies

kglad
Community Expert
Community Expert
January 20, 2009
you're welcome.
pbesongAuthor
Inspiring
January 20, 2009
Yes, I was only giving an example. As stated in my original post, I have 73 buttons, so you can understand my incentive to use a modular function. Thanks again for your help.
pbesongAuthor
Inspiring
January 19, 2009
I was getting NaN when I traced the output of what you had, so looking up substrings, I got this to work:

function goLink(e:MouseEvent){
var b:SimpleButton = SimpleButton(e.currentTarget);
counter = Number(b.name.slice(4,6));
trace(counter);
gotoAndStop(2);
}

Seems to work fine now and I can use it for all the links. Thanks, kglad!
kglad
Community Expert
Community Expert
January 19, 2009
then you had buttons other than the one's you showed. but all's well that ends well. (and you're welcome.)
kglad
Community Expert
Community Expert
January 19, 2009
correct and correct.


function goLink(e:MouseEvent){
var b:SimpleButton = SimpleButton(e.currentTarget);
gotoAndStop(Number(b.name.substring(2)));
}
pbesongAuthor
Inspiring
January 19, 2009
Not quite sure I get what you're saying. So I can't pass a parameter in the event listener for a simple button? I guess I should have (evt:MouseEvent) in the function, but not sure how you pass the number from the simple button.
kglad
Community Expert
Community Expert
January 19, 2009
the 2nd parameter in your listeners must be a function name (without parameter). your function must include a parameter that's consistent with the event.

if your buttons are movieclip buttons, you can give each a property to reference their number or if they are simple buttons, use their name property and the flash string methods to extract the button number.
Participant
January 20, 2009
quote:

Originally posted by: kglad
the 2nd parameter in your listeners must be a function name (without parameter). your function must include a parameter that's consistent with the event.

if your buttons are movieclip buttons, you can give each a property to reference their number or if they are simple buttons, use their name property and the flash string methods to extract the button number.


I have the same issue as described by pbesong but I'm using Movieclips - Can you go into more detail about this solution for Movieclips....
kglad
Community Expert
Community Expert
January 20, 2009
wwc, for movieclip buttons: