1

这在其他语言中似乎很简单,但我不明白这个错误。我有 7 个按钮,当您单击它们时,我希望每个按钮将我的画廊影片剪辑带到某个帧。

错误:1067:将 int 类型的值隐式强制转换为不相关的类型 flash.events:MouseEvent。
错误:1136:参数数量不正确。预计2。
错误:1067:将 void 类型的值隐式强制转换为不相关的类型 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));
4

3 回答 3

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. 简单但重复:为每个按钮使用单独的处理程序,将框架硬编码到每个处理程序中。

    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);
    }
    
于 2013-02-02T04:50:14.123 回答
2

改变这个

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

等等...

于 2013-02-02T11:18:42.443 回答
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.
    }
}

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

于 2013-11-16T02:08:27.133 回答