【问题标题】:AS3 addEventListener to movieclip inside another movieclipAS3 addEventListener 到另一个movieclip 中的movieclip
【发布时间】:2012-08-08 10:22:12
【问题描述】:

所以,我有一个名为“LoginScreen”的影片剪辑,其中有一个名为“confirmbutton”的实例。

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

这是我的代码:

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.

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

【问题讨论】:

  • 您确定 loginscreen 是 MovieClip 吗?也许是 GraphicButton
  • 两者都是电影剪辑。我基本上只为所有内容制作电影剪辑,以便为动作脚本提供额外的灵活性。

标签: actionscript-3 flash event-handling movieclip


【解决方案1】:

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

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

然后您可以像这样从 LoginScreen 对象访问该 MovieClip:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 这就是为什么您必须始终使用首字母大写来命名链接和类。
    • 哎呀。我通常完全相反。小写字母为 AS 链接,首字母大写为 MovieClips。
    猜你喜欢
    • 2012-02-23
    • 1970-01-01
    • 2015-11-07
    • 2017-06-25
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多