将参数传递给mouseEvent函数

时间:2013-02-02 03:47:23

标签: actionscript-3 flash mouseevent movieclip

这在其他语言中看起来很简单,但我不明白错误。我有7个按钮,当你点击它们时,我想把我的画廊动画片段带到某个框架。

Error: 1067: Implicit coercion of a value of type int to an unrelated type flash.events:MouseEvent.
Error: 1136: Incorrect number of arguments.  Expected 2.
Error: 1067: Implicit coercion of a value of type void to an unrelated type Function.

任何帮助?

function gotoImage(event:MouseEvent, frameParam:int):void
{
MovieClip(this.root).gallery.gotoAndStop(frameParam);
}


t1.addEventListener(MouseEvent.CLICK, gotoImage(1));
t2.addEventListener(MouseEvent.CLICK, gotoImage(2));
t3.addEventListener(MouseEvent.CLICK, gotoImage(3));
t4.addEventListener(MouseEvent.CLICK, gotoImage(4));
t5.addEventListener(MouseEvent.CLICK, gotoImage(5));
t6.addEventListener(MouseEvent.CLICK, gotoImage(6));
t7.addEventListener(MouseEvent.CLICK, gotoImage(7));

3 个答案:

答案 0 :(得分:3)

您的代码已经解决了两件事:

  1. 首先,在ActionScript中,事件处理程序始终具有相同的签名:

    function someHandler(e:Event):void { .. }
    

    有时Event参数是Event的更具体的子类,例如MouseEvent,但总是只有一个参数。

  2. addEventListener方法本身需要一个函数,而不是调用函数的结果。

    // Here's a function:
    function multiply(i1:int, i2:int):int { return i1 * i2; }
    
    // Here's assigning the result of **invoking** a function:
    var result:int = multiply(2,3); 
    
    // Here's assigning a **function itself** to a variable:
    var f:Function = multiply;
    
    // You can invoke the function via the variable f in two different ways:
    var result1 = f(2,3);
    var result2 = f.apply(null, [2,3]);
    
  3. 因此,您需要更改代码以遵循上述要点。您必须将按钮与以两种方式之一跳转到特定帧相关联:

    1. 简单但重复性:为每个按钮使用单独的处理程序,并将框架硬编码到每个处理程序中。

      1a上。命名函数(最详细):

      function onT1Click(e:MouseEvent):void {               
          MovieClip(this.root).gallery.gotoAndStop(1);
      }
      
      t1.addEventListener(MouseEvent.CLICK, onT1Click);
      // etc. etc.
      

      1b中。匿名函数:

      t1.addEventListener(MouseEvent.CLICK, function(e:Event):void { 
          MovieClip(this.root).gallery.gotoAndStop(1);
      });
      // etc. etc.
      
    2. 更优雅:使用相同的处理程序,并将按钮和框架之间的关联存储在其他位置,例如在词典中。如果您坚持使用命名约定,您甚至可以在for循环中填写字典,按名称获取按钮:

      var buttonToFrame:Dictionary = new Dictionary();
      for(var i:int = 1; i < 8; i++) {
          var btn:Button = this["t" + i.toString()];
          buttonToFrame[btn] = i;
          btn.addEventListener(MouseEvent.CLICK, onClick);
      }
      
      function onClick(e:MouseEvent):void {
          var btn:Button = Button(e.currentTarget);
          var frameNum:int = buttonToFrame[btn];
          MovieClip(this.root).gallery.gotoAndStop(frameNum);
      }
      

答案 1 :(得分:2)

只需更改此

t1.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{ gotoImage(me, 1)});
t2.addEventListener(MouseEvent.CLICK, function(me:MouseEvent):void{ gotoImage(me, 2)});

依旧......

答案 2 :(得分:0)

这可以通过迂回方法实现。对于事件处理程序,使用函数返回嵌套的匿名函数

private var textFieldA:TextField = new TextField;
private var textFieldB:TextField = new TextField;

public function setParameterizedTextWhenTextFieldsAreClicked ():void {
    addChild(textFieldA);
    textFieldA.text = 'Text field A';
    textFieldA.addEventListener(MouseEvent.CLICK, showCustomMessage("One"));

    addChild(textFieldB);
    textFieldB.text = 'Text field B';
    textFieldB.y = 20;
    textFieldB.addEventListener(MouseEvent.CLICK, showCustomMessage("Two"));
    // NOTE: We must use strongly referenced listeners because weakly referenced 
    // listeners **will get garbage collected** because we're returning
    // an anonymous function, which gets defined in the global namespace and  
    // thus, the garbage collector does not have anything pointing to it.
}

private function showCustomMessage (message:String):Function {
    // NOTE: You can store the following function to a class variable
    // to keep it in memory, which would let you use weakly referenced 
    // listeners when using this as an event handler. Many people 
    // would find that awkward. I would discourage that.
    return function (e:MouseEvent):void {
        var textField:TextField = e.target as TextField;
        textField.text = message; // "message" argument is available because 
                                  // this function's scope is kept in memory.
    }
}

请记住,匿名函数的使用以及对内存中保留的函数范围的依赖似乎会带来垃圾收集的复杂性。