AS3 addEventListener到另一个movieclip内的movieclip

时间:2012-08-08 10:22:13

标签: actionscript-3 flash event-handling movieclip

所以,我有一个名为“LoginScreen”的movieclip,里面有一个名为“confirmbutton”的实例。

我想将LoginScreen添加到舞台并将事件侦听器设置为其中的按钮,但我一直收到错误。

这是我的代码:

var LoginScreen:loginscreen = new loginscreen;
LoginScreen.x = stage.stageWidth / 2;
LoginScreen.y = stage.stageHeight / 2;
addChild(LoginScreen);

LoginScreen.confirmbutton.addEventListener(MouseEvent.CLICK, test);

function test(e:MouseEvent):void{
    trace("Sup?");
}

我收到错误:

Symbol 'LoginScreen'    1046: Type was not found or was not a compile-time constant: confirmbutton.

我很确定它存在且命名正确(具有没有大写字母的确切名称),所以我猜它可能是一个范围问题。

2 个答案:

答案 0 :(得分:0)

想出来。将按钮重命名为“confirmButton”(大写“B”)并开始工作。我想问题是“确认”也是影片剪辑的AS连接。

答案 1 :(得分:0)

尝试使用helper gettter函数获取内部MovieClip。因此,在您定义LoginScreen类的位置放置一个简单的getter函数,如下所示:

public function get ConfirmButton():MovieClip { return this.getChildByName("confirmButton") as MovieClip; }

然后您可以从LoginScreen对象访问该MovieClip,如下所示:

LoginScreen.ConfirmButton.addEventListener(MouseEvent.CLICK, test);