Create Empty MovieClip Dynamically in AS3

Here is how to dynamically create a movie clip in ActionScript 3 using the MovieClip class

First you have to declare the movie clip:
var myMC:MovieCLip = new MovieClip();

Than you have to add it to the stage using addChild:
addChild(myMC);

You can create MovieClips, Sprites, and Objects by calling the word new

Here is an example of the AS2 syntax:

var myMC:MovieClip = _root.createEmptyMovieClip("mc1", _root.getNextHighestDepth());

But for AS3, remember that after declaring your MovieClip it remains in memory and won’t be visible on stage until you call addChild(YOUR_MOVIE_CLIP_NAME)

Here is the AS3 example with property settings:

var myMC:MovieClip = new MovieClip();
myMC.x = 50;
myMC.y = 50;
myMC.name = "mc1";
 
addChild(myMC);

3 Responses

  1. pipz

    how about placing a single movie clip on 2 movie clips let’s say I have a movie clip named imgContainer, and it has a set of images in it.

    for(i=0;i < 2;i++){

    var myMC:MovieClip = new MovieClip();
    myMC.x = 50;
    myMC.y = 50;
    myMC.name = "mc_" + i;

    myMC.addChild(imgContainer);

    }

    addChild(myMC);

    Basically, I want to append 2 image containers in this scenario, but it won't just happen. Not sure why and how to do it in AS3. Thanks!

    • admin

      Did you first declare “imgContainer” before your loop.

      var imgContainer:MovieClip = new MovieClipFromLibrary();

  2. Plinio

    It seems really unintuitive to position the clip before adding it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Next Post » »