2013-02-02 219 views
1

这在其他语言中看起来很简单,但我不明白错误。我有7个按钮,当你点击它们时,我希望每个按钮都将我的图库动画片段带到某个框架。将参数传递给mouseEvent函数

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

回答

2

只要改变这个

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

等等...

+0

这确实有用......你能解释一下这里发生了什么吗? – Gisheri

+0

@Gisher MouseEvent处理程序只接受1个参数。你不能用2个参数创建。所以,我写了一个内联函数作为处理函数,并从这个处理函数调用你的函数。 – skabir

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

所以,你需要改变你的代码,按照上面的点。你必须的按钮与跳转到特定帧的两种方式之一进行关联:

  1. 简单而重复的:使用一个单独的处理程序为每个按钮,用硬编码到每个处理程序框架。 1)。命名功能(最详细):

    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. 更优雅:使用相同的处理,而且,存储按钮和帧之间的关联性的其他地方,如在Dictionary。如果你坚持你的命名约定,你甚至可以填补字典中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); 
    } 
    
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. 
    } 
} 

请记住,使用匿名函数和依赖函数作用域被保留在内存中似乎表现为垃圾收集的复杂性。