So I’ve been embedding stuff in AS3 for like 10 years. (give or take a few years 🙂 ) And I think I’ve pretty much always done it like:
[as][Embed(source=“some.swf”, symbol=“someSymbol”)]
private var SomeThing:Class;[/as]
And never had a problem with it.
On a project I’m starting though, I have a whole bunch of assets on the stage (well over 200), all laid out just like I want them. I don’t want to embed each individual asset and instantiate each one and position it. So I just embedded the whole damn SWF:
[as][Embed(source=“some.swf”)]
private var SomeThing:Class;[/as]
Turns out this is a whole different animal. So I instantiate this thing and try to look inside.
[as]var thing:MovieClip = new SomeThing() as MovieClip;
addChild(thing);[/as]
It has a single child, of type Loader. Hmmm… ok. Loader must have content. But content is null. Yet, there is my content showing up on stage. Dig around a bit more. There’s just nothing in there.
Well ok… I’ll search Embed AS3 on Google. Top hits for that go back to my own damn blog. 🙂 I read my entries anyway, in case I have answered my own question. No luck.
Plan B: Check EAS 3.0 by Moock. Turns out when you embed a whole SWF, it actually comes in as an mx.core.MovieClipLoaderAsset. OK, well, if it’s a loader type thing, then it must be loading something and maybe I need to wait for it to load. I try:
[as]thing = new SomeThing();
thing.addEventListener(Event.COMPLETE, onComplete);[/as]
Woot! It works. Now, thing’s first child is a loader which has valid content which is a MovieClip, which has all my movie clips on it.
[as]private function onComplete(event:Event):void
{
var loader:Loader = _map.getChildAt(0) as Loader;
for(var i:int = 0; i < (loader.content as DisplayObjectContainer).numChildren; i++) { var mc:MovieClip = (loader.content as DisplayObjectContainer).getChildAt(i) as MovieClip; trace(mc.name); } }[/as] Yep. That works. I’ll clean up the code, but I’m on track now. Still not sure why this MovieClipLoaderAsset contains a Loader instead of just containing content directly. Doesn’t seem like it’s supposed to do that. But it does. Interesting anyway. Apparently it creates this loader and then loads the bytes internally, right from the embedded data. I would have thought that like embedding a symbol from a SWF, it would be instantly accessible, but there is a slight delay. Good to know. Now, when I search again in another year or two, maybe I’ll find this post. 🙂 Oh, and before anyone suggests it, I know I could have packaged up the stuff on stage into its own MovieClip and export that and embed that one clip. I was just trying to figure out what was going on here.