开发者

Reference from the DocumentClass a movieclip created by an instance of another class

I have this main (document class) in a Flash project:

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

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

This one is Other class:

    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);
            }
        }
}

Now: if I pass a reference (ref) of the main class to Other and I add clippo as you can see in the first case, I can reference the movieclip clippo from the main (getChildAt(0) is "clippo" from the main). But, is there any way to use the second method (no ref) and do the same from the main class? I can see clippo onstage when Other creates i开发者_如何学JAVAt but I can't understand where clippo "lives" into the DisplayList.


I'm not exactly sure what you're trying to accomplish, but:

  1. You need to add other to the display list if you want to see it or its children.

    public function main()
    {
        var other:Other = new Other(this);
        addChild(other);
    }
    
  2. You can use root instead of passing a reference to the document class. (Once it's on the display list).

    root.addChild(clippo);
    

    But, it makes more sense to just add it to Other: addChild(clippo)

  3. Where you create DisplayObjects doesn't affect the display list, only calling addChild does. Calling addChild in the document class, and root.addChild in Other result in the same display list.


If you are having this sort of issue, you may need to rethink your architecture.

Make some rules that you follow to make things easier on your self, such as; Don't let child element add children to there parents.

I think the best way to get the reference to your clippo DisplayObject, is to provide a getter method for it.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜