【问题标题】:Reference from the DocumentClass a movieclip created by an instance of another class从 DocumentClass 引用由另一个类的实例创建的影片剪辑
【发布时间】:2011-08-21 16:20:20
【问题描述】:

我在 Flash 项目中有这个主(文档类):

package  {
    import flash.display.MovieClip;
    import flash.events.Event;
    public class main extends MovieClip {

        public function main() {
            var other=new Other(this);
        }
    }   
}

这是其他类:

    package  {
        import flash.display.MovieClip;
        import Clippo;

        public class Other extends MovieClip {
            //
            public function Other(ref) {
                //
                var clippo = new Clippo();
                clippo.name="clippo";
                clippo.x=100;
                clippo.y=100;
                //1
                //ref.addChild(clippo);
                //2
                addChild(clippo);
            }
        }
}

现在:如果我将主类的引用 (ref) 传递给 Other 并添加 clippo,如您在第一种情况下看到的那样,我可以从主类引用影片剪辑 clippo(getChildAt(0) 是来自主要的)。但是,有没有办法使用第二种方法(无参考)并在主类中做同样的事情?当其他人创建它时,我可以在舞台上看到 clippo,但我无法理解 clippo “居住”在 DisplayList 中的哪个位置。

【问题讨论】:

  • 您可以将Clippo 的实例添加到Other 的实例中(就像您一样),然后简单地将Other 的实例添加到main 的实例中,就像这样var other = new Other(); addChild(other); .我打算给出这个作为答案,但我觉得即使你的 Other 对象是一个 MovieClip 对象,它也从来没有打算这样使用,我想你可能扩展 MovieClip 当你没有没必要,但我可能是错的。

标签: actionscript-3 document-class


【解决方案1】:

我不确定您要完成什么,但是:

  1. 如果您想查看它或其子项,您需要将other 添加到显示列表中。

    public function main()
    {
        var other:Other = new Other(this);
        addChild(other);
    }
    
  2. 您可以使用root 而不是传递对文档类的引用。 (一旦它出现在显示列表中)。

    root.addChild(clippo);
    

    但是,将其添加到其他更有意义:addChild(clippo)

  3. 在哪里创建 DisplayObjects 不会影响显示列表,只有调用 addChild 会。在文档类中调用addChild,在Other 中调用root.addChild 会产生相同的显示列表。

【讨论】:

    【解决方案2】:

    如果您遇到此类问题,您可能需要重新考虑您的架构。

    制定一些您遵循的规则,以使您自己更轻松,例如;不要让子元素将子元素添加到父元素中。

    我认为获取对您的 clippo DisplayObject 的引用的最佳方法是为其提供一个 getter 方法。

    public function get clippo():Clippo
    {
        return this.clippo;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2010-10-28
      • 2011-01-19
      • 1970-01-01
      相关资源
      最近更新 更多