PDA

View Full Version : Movie Clips AttachMovie and RemoveMovieClip in CS3



EssenceLord
August 28th, 2008, 10:28 AM
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 );
}
}
}
3) Now in you fla, import this class:

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

4) 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...

lionlancer
October 14th, 2008, 10:27 AM
Wow that's great! Can you show a fully working sample for it, please? :)
And is possible to use it on AS files?

Thanks!