【问题标题】:AS3 Accessing MovieClip when added to stage on another AS class file当添加到另一个 AS 类文件的舞台时,AS3 访问 MovieClip
【发布时间】:2015-07-11 02:42:53
【问题描述】:

我已经尝试在这里搜索文章,遗憾的是,没有找到任何有助于解决我的问题的内容。抱歉,如果有文章包含我正在寻找的答案,并且我确信这是一个简单的答案,我就是无法完全理解它。

我有我的 AS 阶段文档和两个 AS 类文件(MAIN 和 gameScreen),我正在尝试从 gameScreen.as 文件中的 MAIN 访问“currentLocation”变量。

我尝试的一切似乎都失败并出现错误。

          import Main;
          trace(Main.currentLocation);

          trace(MovieClip(parent).currentLocation);

我得到的错误:

        TypeError: Error #1034: Type Coercion failed: cannot convert Main$ to flash.display.MovieClip.

请让我知道我哪里出错了,在此先感谢各位。

【问题讨论】:

    标签: actionscript-3 class movieclip


    【解决方案1】:

    只需将其传递给构造函数(类名应以大写字母开头):

    public function GameScreen (currentLocation:Point)
    {
        //...
    

    这不依赖于可能发生变化的显示层次结构(如.parent 所做的那样)。 它也不关心该变量的定义位置(如root.currentLocationstatic var.parent 做)。

    它只是说“你好,我是 GameScreen,我需要当前位置。请给我。”

    【讨论】:

      【解决方案2】:

      我使用这段代码从主 swf 中获取变量

      var p:MovieClip = this.parent.parent as MovieClip;
      trace(p.variableName);

      【讨论】:

        【解决方案3】:

        将 Main 类中的 currentLocation 变量设为静态。

        public static var currentLocation:int;
        

        这样您就可以在 gameScreen 类中访问Main.currentLocation

        【讨论】:

          猜你喜欢
          • 2018-04-24
          • 1970-01-01
          • 1970-01-01
          • 2013-01-20
          • 1970-01-01
          • 1970-01-01
          • 2013-11-25
          • 1970-01-01
          • 2011-01-07
          相关资源
          最近更新 更多