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.

Tags: ,

6 people have left comments

  • raynner - Gravatar
    raynner
    24. 3. 2008

    i have a problem.

    i have a swfA and i load swfB in swfA but i cant access my Foo movieClip?

    can someone send me a tutorial about this?

    thanks

  • admin - Gravatar
    admin
    31. 3. 2008

    Hi,

    do you have the needed movieclip or asset linked in the library?

    We are working with this method almost on a daily basis and it works fine …

  • Ebay hot items - Gravatar 10. 8. 2008

    Very interesting blog, i have added it to my fovourites, greetings

  • lopatnik - Gravatar 25. 10. 2008

    CSS Browser Selector is a very small javascript with just one line and less than 1kb which empower CSS selectors. It gives you the ability to write specific CSS code for each operating system and each browser.

    You can download it at http://rafael.adm.br/css_browser_selector/

    Good work

  • lopatnik - Gravatar
    lopatnik
    25. 10. 2008

    Good books are tough to find, so I’ve begun keeping track of notable ones. I still get a lot of emails asking about graphic design reading recommendations, so when I relaunched this site, I decided to dedicate a section to others like me where I could keep a list of books that I’ve found helpful. I don’t provide these as a comprehensive canon, but as a source of books I’ve vetted as worthwhile for those wanting to keep learning. Enjoy!

  • admin - Gravatar 25. 10. 2008

    C’est trop interessant.

Please Leave a Comment: