AS3 将事件侦听器添加到另一个影片剪辑中的影片剪辑



所以,我有一个名为"LoginScreen"的电影剪辑,里面有一个名为"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.

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

尝试使用帮助程序 gettter 函数来获取内部影片剪辑。因此,在您定义 LoginScreen 类的地方放置了一个简单的 getter 函数,如下所示:

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

然后,您可以从 LoginScreen 对象访问该影片剪辑,如下所示:

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

通了。将按钮重命名为"确认按钮"(大写"B")并开始工作。我想问题在于"确认按钮"也是电影剪辑的AS链接。

相关内容

最新更新