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.
Thank you soooooooooooooo much. We got stuck for a couple of hours until we read this and realized we needed to wait for the embed to load.
I had a poke around at this just the other day and came to the same conclusion. The reason why it used a Loader instance didn’t really cross my mind until I read your post just now.
I did a quick test and sure enough – by using a Loader instance, it keeps all the timeline code! How cool is that? (Why it doesn’t do that in the first place, I don’t know, but hey)
Owen
So, I take it this is a flex project? I’m used to AS3 in Flash, but have never used the [embed] tag. Sorry if this is a uninformed question π
Ahhh…thank God for you( and Moock ;)!!
James I’m pretty sure Keith will be referring to an AS 3.0 project. You can use the embed metadata in AS 3.0 projects.
Keith, looked at our library classes?
http://www.tink.ws/blog/library-librarymanager/
An other post on that topic…
http://www.gskinner.com/blog/archives/2007/03/using_flash_sym.html
Just thinking about importing / embedding an external SWF triggers a red alarm light in my head. It’s always been a bit buggy and unstable – I’ve had some issues before, so now I’m kind of afraid of it. I stay away from importing / embedding external SWFs. But that sounds right. I thought it was funny, Keith, how you explained the whole scenario – you had a problem, went for answers but wound up back at the start (your blog), and solved it, but then decided to share it all with everyone. π Yay. Hehe. Take care.
another simple but powerful example for whatΓΒ΄s wrong with the flash platform “evolution” π
Take one of the most quick and simple things to do in older flash versions and add as many unnessecary levels of cumbersome to use complexity as possible until it almost feels like a special achievment to have accomplished a laughable task.
Ah… shake n’ bake swfs… I’m surprised no one has mentioned exporting swc’s from flash… Everything comes through strong typed. Excuse me Keith, I do understand that this post is just on what exactly happens when you embed a swf, but I thought I would add to the alternatives discussion by bringing swc’s to the readers attention…
Keith showed me this in Boston and I’ve used it tons since – I’ve not had any issues with this technique. I love the ability to embed assets like this. Embedding collada and bitmaps for Papervision stuff rocks π
Hi,
I did find it useful that you walked us through your discovery process, but I am having issues producing working code with this, and it’s not entirely clear which lines I should leave in or out. Is there anyway we could see a complete example of this thats working and perhaps also accessing something in the embedded swf itself from flex or AS?
Thanks for your post!
I have a lot of swf’s embedded this way and I need about 15 of them loaded at some point. What I’m doing is:
var counter = 0;
counter++;
x = new EmbeddedClass();
x.addEventListener(…, onCompleteFunc);
function onCompleteFunc(e : Event) : void
{
// do stuff with loaded data
counter–;
if (counter == 0)
// everything is loaded, notify main app driver
}
It works perfectly on slow, single-core machine, but on my personal one (2.4GHz dual-core – I think dual-core is most important) I receive only about half of notifications – it seems that data is loaded faster than I’m establishing listeners! Does anyone has any idea what can be done about it?
”
//——-
…
var loader:Loader = _map.getChildAt(0) as Loader;
…
/———
”
1. what is _map here and why the child at pos 0 is Loader?
I have been examited what came in currentTarget and it was embeded MC and its “zero” child was also Loader… And now i have the same question – why its child is Loader?