Flash 按钮点击不起作用

Flash Button clicks don't work

我有 4 个相同的基本按钮对象实例(一个矩形)。按钮 Class 称为 LinkBut​​ton,名称为 LinkBut​​ton,每个实例为 button_1、_2、_3 和 _4。我正在使用以下 ActionScript:

import flash.net.navigateToURL;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import fl.motion.MotionEvent;

this.button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage("http://google.com"));
this.button_2.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage("http://adobe.com"));
this.button_3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage("http://amazon.com"));
this.button_4.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage("http://ask.com"));

function fl_ClickToGoToWebPage(url:String) {
    navigateToURL(new URLRequest(url), "_blank");
}

当我从 Web 服务器发布和 运行 时,一个新的(空白)页面打开 google.com - 我没有点击,它只是自动打开。 None 其他页面打开,但是。当然,我真的只希望这些 URL 在点击事件时打开。我在这里做错了什么?

这样做的原因是,当您为事件添加回调时,您将其传递给函数的引用。

然而,您正在做的是传递函数的 result。 (所以在你添加监听器的时候,你实际上是 运行 方法 fl_ClickToGoToWebPage 和提供的值。)

你要的是这个:

this.button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage);

您可以通过几种不同的方式传递数据 (url)。

  1. 在处理程序中使用 switch 语句:

    这个解决方案非常好,它很简单,但可能不像其他方法那样封装。

    //you get a reference to the button that was clicked with the event's current target property.
    function fl_ClickToGoToWebPage(event:Event) {
        var url:String;
        switch(event.currentTarget){
            case button_4:
                url = "http://ask.com";
                break;
    
            case button_3:
                url = "http://amazon.com";
                break;
    
            //etc for the other buttons
        }
        navigateToURL(new URLRequest(url), "_blank");
    }
    
  2. 为每个按钮分配动态 属性。

    这个解决方案是最封装的,因为数据附加到实际的按钮,但动态变量容易出现拼写错误等,并且不会被编译器检查。

    //if button_1 is a MovieClip or other dynamic class, you can just make up a property on it like this:
    this.button_1.url = "http://google.com";
    this.button_1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToWebPage);
    
    //..repeat for the rest of the buttons
    
    
    //then, in the handler, the event.currentTarget is a reference to the button that was clicked:
    
    function fl_ClickToGoToWebPage(event:Event) {
        navigateToURL(new URLRequest(event.currentTarget.url), "_blank");
    }
    
  3. 为您的 LinkButton 创建一个 class 文件并添加一个 public url 属性

    这是最优雅的解决方案,但对于一个 属性 来说可能有点矫枉过正。创建一个名为 LinkButton.as 的文件并将其放在您的 .fla.

    旁边

    将此添加到文件中:

    package {
        public class LinkButton extends MovieClip {
            public var url:String;
        }
    }
    

    不过,更好的是将 LinkButton 的所有功能封装到它自己的 class 文件中:

    package {
        public class LinkButton extends MovieClip {
            public var url:String;
    
            public function LinkButton(){
                this.addEventListener(MouseEvent.CLICK, clickHandler);
            }
    
            private function clickHandler(e:MouseEvent):void {
                if(!url) return;
                navigateToURL(new URLRequest(url), "_blank");
            }
        }
    }
    
    //this way, all you have to do in your timeline is set the url of each button.
    

    现在你有一个 url 属性 用于所有 LinkButton,使用这个 属性 与以前的解决方案相同。不同之处在于,现在您可以进行编译时检查。

  4. 使用烦人的函数作为 return 值

    如果您需要动态添加侦听器或需要 remove/re-add 个侦听器,此解决方案很危险并且容易出现内存泄漏。

    想法是,您使用参数调用一个函数,然后 return 另一个函数用作处理程序。

      this.button_1.addEventListener(MouseEvent.CLICK, createButtonHandler("http://google.com"));
    
      function createButtonHandler(url:String):void {
          return function(e:Event){
              navigateToURL(new URLRequest(url), "_blank");
          }
      }
    

    这里的问题是,您无法再次引用此函数。因此,即使您从屏幕上删除按钮并将其占位符 (vars) 设为空,它也会永远保留在内存中。