首页 > 解决方案 > Animate ActionScript 3.0 中的按钮超链接

问题描述

我正在尝试使用 ActionScript 在 Adob​​e Animate 中制作三个按钮。这是我用于按钮 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/"));
}

(这些 URL 是编造的,仅用于演示目的。)如您所见,按钮 2 和 3 的代码完全相同,但位于不同的层。我还在开始时更改了实例名称、函数名称和每个按钮的 URL。但是当我按下 CTRL + Enter 时,所有按钮都指向同一个网页(在本例中为“website.com”),这是我首先输入的网页。它应该导致我输入的不同 URL,但它们都指向同一个。为什么会发生这种情况,我该如何解决?

标签: actionscript-3adobe-animate

解决方案


问题是,尽管您为 3 个单独的按钮定义了 3 个单独的处理函数,但您将第一个处理函数附加到所有 3 个按钮:

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

你的意思是这样的:

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

不过,顺便说一句,您可以使用事件的当前目标参数使用一个处理函数来完成这一切,以确定单击了哪个按钮:

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));
}

推荐阅读