0

I'm trying to make three buttons in Adobe Animate, using ActionScript. Here is the code I'm using for button 1:

button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void {
     navigateToURL(new
     URLRequest("https://website.com/"));
}



button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler2(event:MouseEvent):void {
     navigateToURL(new
     URLRequest("https://anotherwebsite.com/"));
}


button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler3(event:MouseEvent):void {
     navigateToURL(new
     URLRequest("https://yetanotherwebsite.com/"));
}

(The URLs are made up and only for demonstration purposes.) The code for button 2 and 3, as you can see, are exactly the same but on different layers. I also change the instance name at the start, the function name, and the URL for each button. But when I press CTRL + Enter, all the buttons lead to the same webpage (in this case "website.com"), which is the one I put in first. It should lead to the different URLs I put in, but they all just go to the same one. Why is this happening and how do I fix this?

mbjb
  • 111
  • 5
  • There's no problem with the code above. Please edit the question to provide the actual code to comply with MCVE principles: https://stackoverflow.com/help/mcve At the moment the **verifiable** aspect is in question. – Organis May 28 '18 at 10:03
  • Show the code for all 3 buttons, and verify that you instance names in Animate match your code in every relevant frame. – BadFeelingAboutThis May 28 '18 at 16:52
  • I edited the question to make it more clear. @BadFeelingAboutThis, I checked and double-checked the names before I asked this question or I wouldn't have if that was the problem. Thanks for the tip anyways. – mbjb May 29 '18 at 14:04
  • Your problem statement is very clear, thank you, but you have not provided enough information to identify the problem. I'd recommend pasting ALL the code for all 3 layers/buttons – BadFeelingAboutThis May 29 '18 at 18:08
  • @BadFeelingAboutThis there you go, all the code. – mbjb Jun 04 '18 at 12:19
  • Thank you, now the problem is clear, all three of your button have the same `mouseDownHandler` as their event handler – BadFeelingAboutThis Jun 04 '18 at 17:02

1 Answers1

1

The problem is, although you've defined 3 separate handler functions for the 3 separate buttons, you are attaching the first handler function to all 3 buttons:

button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

What you mean to do is this:

button. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
button2. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
button3. addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler3);

Though, as an aside, you could do it all with one handler function using the event's current target parameter to figure which button was clicked:

function mouseDownHandler(event:Event):void {
    var url:String;
    //event.currentTarget is a reference to the object that you attached the event listener to
    switch(event.currentTarget){
        case button:
            url = "https://website.com/";
            break;

        case button2:
            url = "https://anotherwebsite.com/";
            break;

        default:
            url = "https://yetanotherwebsite.com/";
    }

    navigateToURL(new URLRequest(url));
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Ok, thanks so much! This is my first time using ActionScript, it seems like such a basic mistake! – mbjb Jun 05 '18 at 01:42