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

11 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!

  6. ccleaner Says:

    I’m seriously not too acquainted with this matter but I do like to take a look at blogs for layout strategies and intriguing topics. You extremely described a subject matter that I generally do not care much about and made it fairly fascinating. That is a wonderful weblog that I will be aware of. I currently bookmarked it for future reference. Thank you Ccleaner, ccleaner news

  7. Bryant Riquelme Says:

    I genuinely prize your work , Great post.

  8. Delicia Cory Says:

    I found this webpage on Google blogsearch and read a few of your different posts. This is one thing I’ve extremely wanted to be told more regarding I am finding it laborious to find trustworthy data regarding it, thus thank you for the useful post.

  9. Acid reflux symptoms strategies Says:

    I am definitely not as well accustomed to this subject matter but I do wish to visit blogs for layout ideas and exciting topics. You surely described a that I normally really don’t care considerably about and crafted it especially fascinating. That is a good weblog that I’ll be aware of. Find out tips in our site

  10. Rheinschmidt@hotmail.com Says:

    Amazing! It’s like you comprehend my mind! A person seem to know so significantly relating to this, like you wrote the book in it or even something. We feel which you could do with a few pictures to drive the content house a little, on top of that, this is educational weblog publish. A excellent read. I’ll undoubtedly return.

  11. TrickeyD Says:

    Legend!

Leave a Reply