Moving From Flash AS2 to Flash AS3: Dynamic MovieClip Creation

Creating a MovieClip dynamically  is a task you will find yourself doing often in ActionScript 3.0.  If you’ve ever tried this is AS2, it required the population of many parameters and a frequent trip to the Flash Help panel.  AS3 introduces much simplified solution to dynamically creating a MovieClip.

Dynamic MovieClip Creation in AS2

var container:MovieClip = this.createEmptyMovieClip("container", this.getNextHighestDepth());

Dynamic MovieClip Creation in AS3

var container:MovieClip = new MovieClip();
addChild(container);

Differences?

Simplicity of this AS3 integration makes for less headaches and cleaner ActionScript code.  The key difference is that you no longer have to define a level for the MovieClip to reside on as it is handled by display list.

Hope this helps!

12 thoughts on “Moving From Flash AS2 to Flash AS3: Dynamic MovieClip Creation

  1. “If you’ve ever tried this is AS2, it required the population of many parameters and a frequent trip to the Flash Help panel”

    Sorry but i laughed… if *two* are “many” parameters, then I wonder what it will be when you will explain TextFormat()…

    🙂

    • For your beginner ActionScript user, one might be “many”. I’ve worked with Flash designers learning ActionScript that had a hard time understanding parameters. But I will admit, I chuckled as I wrote it. Maybe I was over selling the new declaration. 🙂

  2. This is rather simple. Howeverm, it’s too bad they didn’t make duplicating existing MovieClips simple. You have to create the desired MovieClip, export it from the library with a custom class name, then:

    var myDuplicateMovieClip:exportedClip = new exportedClip();
    addChild(myDuplicateMovieClip);

    While this is handy for doing things you used to with attachMovie in AS2, this really sucks because there is no way to exactly duplicate already modified MovieClips that are already on the stage with all of their children intact, a la dupilicateMovieClip of AS2. I feel this is a hideous oversight in AS3. Why on earth would they leave something so useful out of the language?

  3. Easy or hard?!?!

    AS2 copied a lot of code from AS1 making it simple to jump from one to the other. The step from AS2 to AS3 seemed waster simply because the code is very different – especially i you don’t have any experience with flex or xml.

    But i guess i all depends on how u coded i AS2 – exemplified by this.

    var xxxx:String = xxxxxx

    vs.

    xxxx=xxxxx

    Personally I found it hard suddenly to change the coding approach.

  4. @Bandits: That was the problem with AS2. It introduced datatype referencing(var myString:String = “”), however, it was too lenient by not enforcing it’s own rules.

    And you are right, if you did not practice strict datatypes, it will be a little confusing. An easy way of seeing it is that you are basically telling Flash what type of data you are storing, that’s it! There is no mystery behind it.

    Also, you can have no experience in XML and/or Flex to understand ActionScript 3.0. That is a complete misconception. Just see AS3 as the stricter parent of AS2.

  5. Hey Angel

    Im not agreeing with you – nor am i complaining. You pretty much sum up what i wanted too say i your last statement.

    I’ve met at lot of people, primarily sub-suppliers, who started coding i flash because of its non-strict nature. There alternative at the time was php4 or js.

    These people never included an .as file on wrote an function. They love the fact that they could draw an _mc an write the script on the _mc.

    When they switch from AS1 to AS2 they quickly learn “the new words” and, when others discover the power of AS2 they still tweened i frames and jump from scenes.

    these people, who only visits forum like these when they have a problem, these people have a hard time changing to AS3 – simply because the syntax i “less forgiving”.

    The reason i mentioned xml and Flex – i could have said php or some other scripting – was, that these language tough me to review my way of scripting and work with shorter, stricter and more function-minded scripts – simply because these were “lesser forgiving” but more powerful and easier to edit.

    AS3 has become a “real” scripting language with the price of loosing people who never obtained that mindset – especially because AS3 and AS2 doesn’t work with each other like AS1 & AS2 do.

  6. @Bandits: Point well taken. I totally understand where you are coming from. I think this is where Adobe(Macromedia) dropped the ball. I am glad AS3 was born, but the way it came about was a bit abrupt.

  7. This helps, but could use more info or links to represented examples. For instance, including loader to allow one to import external movieclip objects from the folder hierarchy not necessarily from the library. Or how to reference movieclips within another movieclip’s timeline once it has been cast as a child object. Once a child object is cast to the stage it is now a displayobject an no longer “usable” for all intensive purposes as an ‘interactive’ object. Even the standard reference .syntax nomenclature goes out the door with addchild – since you can addchild to another movieclip but reference it with the child instance name from anywhere without going through the normal parentInstanceName.ChildInstanceName naming convention.

    I’ve been with Flash since Flash 2 and have had the opportunity to slog through each new version and changes in coding – at its current state it looks to be a great IDE platform for people on different levels, positioned up against MS Dev IDE.

Comments are closed.