0

我正在尝试为按钮数组中的所有按钮添加一个事件侦听器。我可以在循环中制作它们的按钮,但是当我尝试添加事件侦听器时给我这个错误:

TypeError: Error #2007: Parameter listener must be non-null. at flash.events::EventDispatcher/addEventListener() at Main()

我可以将此事件添加到另一个数组,但不能添加到这个数组。我已将这些按钮放在舞台上,并为它们提供了我在 as 文件中引用的实例名称。我在学校学习 AS3,所以这可能是一个非常明显的问题,但我还没有资格调试我的代码:S 感谢您的所有帮助。

//array of buttons and making them buttons
var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];

for(var b:int = 0; b<buttons.length; b++){
    buttons[b].buttonMode = true;
    buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}

function clickMe(e:MouseEvent):void{
    trace("hello");
}
4

2 回答 2

0

检查定义和参数部分中“clickMe”函数的名称,确保使用完全相同的字符(一个字符有时会被误认为是另一个代码表中的字符)。发生错误是因为在执行循环时“clickMe”表达式为空。

var buttons:Array = [armButton, lobeButton, beakButton, crotchButton, earButton, hairButton, legButton, shoulderButton, spineButton, tailButton, tearButton, eyeButton];

for(var b:int = 0; b<buttons.length; b++){
    buttons[b].buttonMode = true;
    // what is the output of the following expression?
    trace(clickMe) // should be "function Function() {}"
    buttons[b].addEventListener(MouseEvent.CLICK, clickMe);
}

function clickMe(e:MouseEvent):void{
    trace("hello");
}
于 2012-12-07T18:21:35.497 回答
0

在您的 addEventListener 行中,clickMe 为空。

我怀疑我们在这里没有看到所有的代码。该代码是否在同一个文件中?还是您只剪切/粘贴您认为重要的部分?

于 2012-12-07T23:43:09.967 回答