Search code examples
actionscript-3flashembedding

Access children of embedded aswf


I am embedding an swf file that has some children on its timeline. Like this:

[Embed(source="assets/skyscraper200x600.swf")]
private var Skyscraper  :Class;

All children in the swf have an instance name, I double checked that when creating the swf in Flash CS5. I am trying to access those children by name like this:

_bg = MovieClip(new Skyscraper());
_pig = MovieClip(_bg.getChildByName("chara_pig"));
_arrow = MovieClip(_bg.getChildByName("arrow_banner"));

However, both _pig and _arrow end up being null.

What's even stranger is that when I look at the Skyscraper object in the debugger, it shows a rather strange class name and a Loader as its only child (which in turn has no children). What's up with this?

.

I can access them like above if I do not embed the swf, but load it with a Loader. But I cannot do it in this case. I need to embed the swf.

So, how can you access children of embedded swfs?

I am not talking about accessing classes in the library of the embedded swf, but the instances on the timeline.


Solution

  • Here is a solution. You can also see the steps who helped me find this solution (describeType is your friend) :

    public class Demo extends Sprite {
    
        [Embed(source="test.swf")]
        private var Test:Class
    
        public function Demo() {
            //first guess is that embed SWF is a MovieClip
            var embedSWF:MovieClip = new Test() as MovieClip;
            addChild(embedSWF);
    
            //well, emebed SWF is more than just a MovieClip...       
            trace(describeType(embedSWF));//mx.core::MovieClipLoaderAsset
            trace(embedSWF.numChildren);//1
            trace(describeType(embedSWF.getChildAt(0)));//flash.display::Loader
    
            var loader:Loader = embedSWF.getChildAt(0) as Loader;
    
            //the content is not already loaded...
            trace(loader.content);//null
    
    
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(){
                var swf:MovieClip = loader.content as MovieClip;
                var child:MovieClip = swf.getChildByName("$blob") as MovieClip;
                //do nasty stuff with your MovieClip !
            });
        }
    }