AS3: Linked MovieClips from remote SWFs.
We’ve had a situation here. It’s irritating. It’s annoying. It happens over and over. It is as follows:
- SWF A wants to load SWF B.
- SWF B has a linked MovieClip in the library. This is a simple extension of MovieClip, nothing fancy. Let’s call it Foo.
- SWF A wants to call new Foo(); and add it to the stage.
- Compiler hates us.
Daniel, Josh and I sat together and had a pow-wow and came up with this:
import flash.utils.getDefinitionByName;
function getAsset($str:String):MovieClip
{
var c:Class = Class(getDefinitionByName($str));
return new c();
};
(Amazingly, it worked out so that I typed lines 1 and 2, Daniel typed lines 3 4 and and Josh lines 5 and 6. Therefore, we all share joint credit in this astounding revelation.)
This function will comb for a class by string, assume it’s a MovieClip and return it. Now we just agreed on a simple convention: All linked movieclips will begin with “lib.”.
So I can easily write:
var myFoo:MovieClip = getAsset("lib.Foo");
addChild(myFoo);
from SWF A once SWF B is loaded. Presto!
Daniel is going to build this into a nice, pretty utility class. When he does, rest assured that we’ll distribute it here.
November 30th, 2007 by Stephen / 13 Comments


