Skip to main content
Participant
October 6, 2009
Question

Buttons to Scene

  • October 6, 2009
  • 1 reply
  • 510 views

I have a scene in which I have 11 buttons all of which I would like to point to diffrent individual scenes.  When placing the following code for the first button it works fine:

stop();

btn1892.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownHandler(event:MouseEvent):void {

gotoAndStop(1, "1892");

}

But then I add the following in for the second button and I get and error (1021 - duplicate function) and neither the first previously working button is functional nor the new one:

btn1904.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownHandler(event:MouseEvent):void {

gotoAndStop(1, "1904");

}

Being so new to Action Script and Flash I'm not sure where to even start troubleshooting.  Could someone lead me in the right direction?

Thanks!

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
October 6, 2009

You cannot use the same function name for different functions.  You need to name each function differently... OR...

What you might want to do is change your frame labels to be the button names, then you could have one function for all the buttons that go to frame 1 of a scene.

function mouseDownHandler(event:MouseEvent):void {

     gotoAndStop(1, event.currentTarget.name);

}

One other thing though, you might want to change the mouse event to be a CLICK rather than a MOUSE_DOWN.

Participant
October 7, 2009

Ned, thank you.

So say I have a button named btn1892 and a frame labled 1892frame what would the code look like? The example you provided me doesn't make a lot of sense to me yet and I just need to know where to plug things in.

Ned Murphy
Legend
October 7, 2009

You will have complicated my proposed approach with the names you offered.  If you name the button btn1892, then also name the scene as btn1892.  Then the event handler function code would look exactly as I had it...

function mouseClickHandler(event:MouseEvent):void {

     gotoAndStop(1, event.currentTarget.name);

}

btn1892.addEventListener(MouseEvent.CLICK, mouseClickHandler);

btn1904.addEventListener(MouseEvent.CLICK, mouseClickHandler);

The "event" argument that gets passed to the function carries a various pieces of information.  One piece of information is the instance of the object that experienced the event (the button that was clicked), and that instance is event.currentTarget.  The name of the currentTarget will be the instance name you assigned it.  So if the scene for that button shares its name, its name can be used to go to that scene.

On another note... do not be surprised if you have trouble navigating scenes with actionscript... just cross your fingers you don't.  They can be problematic when it comes to navigating them with code.