Movie Clips AttachMovie and RemoveMovieClip in CS3

As I am sure you have noticed, attachMovieClip and removeMovieClip have been obliterated from existence in the transition to AS3. So, I’m summoning them back with this:

  1. Create an Actionscript file (*.as) and call it MovieFunctions
2) Add the following code to that file:
package
{
    public class MovieFunctions
    {
        import flash.display.MovieClip;
        import flash.utils.getDefinitionByName;
        
        public function MovieFunctions():void
        {
            
        }
        
        MovieClip.prototype.AttachMovie = function(idName:String, newName:String, level:int, initObject:Object=null)
        {
            var instance = new (getDefinitionByName(idName));
            
            for( var key in initObject ) 
                instance[key] = initObject[key];
            
            if (level)
                this.addChildAt( instance, level );
            else
                this.addChild(instance);
            
            instance.name = newName;
            
            return instance;
        }
        
        MovieClip.prototype.RemoveMovieClip = function ()
        {
            MovieClip(MovieClip(this).parent).removeChild(this);
        }
    }
}
  1. Now in you fla, import this class:

import classes.MovieFunctions;
var MF:MovieFunctions = new MovieFunctions ();

  1. Any time you want to add or remove a movie clip, you can do so as you did in previous versions of flash!

ie:
MovieClip(this).AttachMovie(“MyMovie”, “NewMovieName”, layer, {x:30, y:50});

MyMovie.RemoveMovieClip();

That’s one less headache…