when you’ve loaded other clips into your movie has always been very painful.
You can’t


a asset into a loaded clip that is located in the parent movieclip, and conversly you can’t


a asset in a parent that was loaded into a child.

This is extremely frusterating… but after some searching today I came across this interesting link. Using the fact that Flash likes to cache everything it can, he created a function that will load an clip that contains an asset into the movieclip you are trying to use


Nice idea, never thought of doing this before.

Note: this is only needed if you are loading external movieclips and want to use common assets, yes you could use shared assets but they have always given me a headache (they are a pain to create/maintain, although they may have improved since I’ve last used them), and this seemed like an interesting alternative.

After attempting to use the above mentioned method I found that the code on the site I linked to wasn’t really usable for what I was trying to do, I rewrote it a bit:

 Function: attachMovieAnywhere
 Loads a clip (should have nothing on the stage, containing just assets) and attaches a movieclip from the loaded clip onto the stage
 file - [String] path to the SWF containing the assets
 callBack - [Function] function that will be called once the asset swf is loaded, and the movieclip has been attatched. The scope of the function is the attatched movieclip, and the first argument is the attatched movieclip.
 If you need a different scope use the <delegate> class.
 NOTE: this does not call once the attatched movieclip has been initialized, only when it has been attatched!

MovieClip.prototype.attachMovieAnywhere = function(file:String, idName:String, newName:String, depth:Number, initObject:Object, callBack:Function) {
    if(depth == undefined)
        depth = this.getNextHighestDepth();
    var parent:MovieClip = this;
    var container:MovieClip = this.createEmptyMovieClip(newName, depth);
    var mcLoader:MovieClipLoader = new MovieClipLoader();
    var listener:Object = new Object();
    listener.onLoadInit = function (mc) {
        parent[newName] = mc.attachMovie(idName, newName, mc.getNextHighestDepth(), initObject);
        if(callBack)[newName], parent[newName])
    mcLoader.loadClip(file, container);

Here is some example code showing how it is used:

attachMovieAnywhere("asset.swf", "aAsset", "coolthing", getNextHighestDepth(), {_x:-40, _y:-40}, function(mc) {
    if(mc == this) {
        // mc == this unless you use the Delegate class to change the this var
        trace("They Do!")

    // you can do initialization here
    mc.onRollOver = function() {
    mc.onRollOut = function() {

Average Rating: 4.6 out of 5 based on 245 user reviews.

3 Responses to “”

  • damn… please delete above.

    Ofcourse you cant do:
    var mc:MovieClip = this.attachMovie ();
    mc.onPress = sth..

    onLoad handler has not occured yet.


    class Foo extends MovieClip {
    public var s:String;

    and then:

    var mc:Foo = Foo (this.attachMovie)
    mc.s = “blah”

    has effect

    movie clips, associated with classes are precompile, you might say.

    but thanks Adobe, we’ve got AS3.0

  • Actually, normally if your attatching a movie that is located in the current movie’s library, you can do

    var mc = this.attachMovie()
    mc.onPress = function() {}

    and the onPress event handler will be registered, unless of course your using a custom class with your attatched asset, then its different.

  • Hi,

    This is working nicely, but initObject:Object seems to be ignored in my tests. Any idea what’s up?



Leave a Reply