AS3如何从另一个SWF获得孩子

时间:2013-02-27 23:39:25

标签: actionscript-3 get flash external children

我该怎么做?我一直在使用装载机,然后尝试从那里接收孩子,但是当SWF有4个孩子时,我只收到一个孩子。

1 个答案:

答案 0 :(得分:1)

试试这个:

package
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.utils.getQualifiedClassName;

    public class GetChildren extends Sprite
    {
        public function GetChildren()
        {
            var l : Loader = new Loader;
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
            l.load(new URLRequest("http://staticns.ankama.com/dofus/www//game/items/src/1223.swf"));
        }

        protected function onSwfLoaded(event:Event):void
        {
            var result : Vector.<DisplayObject> = getChildren(LoaderInfo(event.target).content as DisplayObjectContainer);
            trace("Children count : " + result.length);
        }

        private function getChildren(target : DisplayObjectContainer, inc : String = "", result : Vector.<DisplayObject> = null) : Vector.<DisplayObject> {
            if(!result)
                result = new Vector.<DisplayObject>;

            for(var i : uint = 0; i < target.numChildren; ++i)
            {
                var child : DisplayObject = target.getChildAt(i);
                result.push(child);
                displayInfo(child, inc);
                if(child is DisplayObjectContainer)
                    getChildren(child as DisplayObjectContainer, inc + "   ", result);
            }

            return result;
        }

        private function displayInfo(target : DisplayObject, inc : String = "") : void
        {
            trace(inc + target.name +" : " + target + " (" + getQualifiedClassName(target).split("::").pop() + ")");            
        }
    }
}

将输出:

instance3 : [object Shape] (Shape)
instance4 : [object MovieClip] (MovieClip)
   instance5 : [object Shape] (Shape)
instance6 : [object MovieClip] (MovieClip)
   instance7 : [object Shape] (Shape)
instance8 : [object MovieClip] (MovieClip)
   instance9 : [object Shape] (Shape)
instance10 : [object MovieClip] (MovieClip)
   instance11 : [object Shape] (Shape)
instance12 : [object Shape] (Shape)
instance13 : [object MovieClip] (MovieClip)
   instance14 : [object Shape] (Shape)
instance15 : [object Shape] (Shape)
instance16 : [object MovieClip] (MovieClip)
   instance17 : [object Shape] (Shape)
instance18 : [object Shape] (Shape)
instance19 : [object MovieClip] (MovieClip)
   instance20 : [object Shape] (Shape)
instance21 : [object Shape] (Shape)
instance22 : [object MovieClip] (MovieClip)
   instance23 : [object Shape] (Shape)
instance24 : [object Shape] (Shape)
instance25 : [object Shape] (Shape)
Children count : 23