AS2 Cast and attach a Custom MovieClip class

One annoying thing that i found in as2 that has been fixed in as3 is the ability to write: new MyCustomClass() and then attach it to the stage. In AS2 there is no way to instantiate a class (which extends MovieClip) and have it parented to an existing MovieClip, unless you put it in the library with a linkage identifier, and assign the class to that asset. This is an arduous process if the clip doesn’t even require any assets drawn in the IDE.

A useful way to work around this is to have an empty movieClip in the library with a linkage of ‘empty’. Then to instantiate your custom class you must dynamically assign that class to the empty library asset with registerClass. If you then attach the ‘empty’ and cast it as your custom class type, bingo! you have your custom class instantiated on the stage.

registerClass( “empty”, MyCustomClass);

var mc:MyCustomClass = MyCustomClass(attachMovie(”empty”));

With correctly casting the attachMovie() you get correct typechecking and auto-completion in FlashDevelop. Remember, your custom class must extend MovieClip in the .as file

5 Responses to “AS2 Cast and attach a Custom MovieClip class”

  1. Jakob Sternberg Says:

    Is this possible in flash timeline scripting?..i can’t get it to work

    like this:

    this.registerClass(”empty”, Circle);
    var mcCircle:Circle = Circle(attachMovie(”empty”));

    And the movieclip in lib has only a linkage indentifier right?

  2. Tonypee Says:

    To make the class Circle (as in this example) you will need to use AS2. Actually doing the registerClass and attachMovie - this should work fine on the timeline.
    It definately works, i used to do this all the time - now i just use as3

  3. sandrar Says:

    Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  4. angelina jolie Says:

    I love your site. :) Love design!!! I just came across your blog and wanted to say that I?ve really enjoyed browsing your blog posts. Sign: ndsam

  5. megan fox Says:

    Sign: umsun Hello!!! rcuwwymhyw and 4765ssgfhphzye and 9518I like your blog. cool post!

Leave a Reply