AttachMovie & Loaded Clips

Using attachMovie when you’ve loaded other clips into your movie has always been very painful.
You can’t attachMovie a asset into a loaded clip that is located in the parent movieclip, and conversly you can’t attachMovie 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 attachMovie from.
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:
[code lang=”actionscript”]
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 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:
[code lang=”actionscript”]
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() {