A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: how to create an instance of class extending movieClip in AS2.0?

  1. #1
    Senior Member
    Join Date
    Jun 2001
    Posts
    133

    how to create an instance of class extending movieClip in AS2.0?

    In Action Script 2.0, how do you create an instance of a class that extends the MovieClip class and put it on the screen?


    So I have a class below that extends MovieClip that simply draws a triangle. How do programmatically create an instance and put it on screen? I tried createClassObject() but that didn't work.

    class MyMovieClipClass extends MovieClip
    {
    // Constructor
    function MyMovieClipClass()
    {
    this.drawMe();
    }


    // Draw a simple triangle for the heck of it.
    private function drawMe():Void
    {
    this.clear();
    this.lineStyle(10, 0xCCCCCC);
    this.lineTo(300,0);
    this.lineTo(300,200);
    this.lineTo(0,0);
    }

    }

  2. #2
    Senior Member
    Join Date
    May 2004
    Posts
    182
    i found no better solution than to put an empty movie in to the library and changing the class there, and then using attachMovie().

  3. #3
    Senior Member
    Join Date
    Mar 2000
    Posts
    235
    You don't need and shouldn't use inheritance to do this.
    try;

    class MyMovieClipClass {
    //Properties
    private var containerMC:MovieClip;

    {
    // Constructor
    function MyMovieClipClass(target:MovieClip)
    {
    containerMC = target.createEmptyMovieClip("container"+getNextHig hestDepth(),getNextHighestDepth());
    this.drawMe();
    }

    // Draw a simple triangle for the heck of it.
    private function drawMe():Void
    {
    containerMC .clear();
    containerMC .lineStyle(10, 0xCCCCCC);
    containerMC .lineTo(300,0);
    containerMC .lineTo(300,200);
    containerMC .lineTo(0,0);
    }

    }

    containerMC has all the properties and methods of MovieClip and can be accessed via any new instance of MyMovieClipClass such as;

    var someMC:MyMovieClipClass = new MyMovieClipClass(this);
    someMC.containerMC._visible=false;


    Hope this helps

    rosie

  4. #4
    Senior Member
    Join Date
    Jun 2001
    Posts
    133
    The code was just an example, I do not really need to make a square, nor a better way to make a square, thanks though. )

    My question is, how do I instantiate a class that extends the class MovieClip, or is that just not allowed now in ActionScript 2.0? This used to be allowable and was a basic building block if you wanted a class to inherit basic properties for an object that goes in the timeline.

    I just figured the way to instantiate and get it on the timeline changed.

    I'm trying to understand what is under the hood.

    Thanks everyone for the help.

  5. #5
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    same as before. Like konsu said, attachMovie is used to instantiate class that inherits from MovieClip. Empty movieclips cannot (through conventional means) be instantiated as custom class instances unless attached from the library using an empty clip manually created there and associated with the class.

    createClassObject is just a wrapper for Object.registerClass and createObject and createObject is just a wrapper of attachMovie.

    In any case, to make an instance of a class inheriting from MovieClip, you will need to make the movieclip, not a class instance. That movieclip then becomes the class instance.

    rosie gave a good alternative for empty clips and how they can be used as class properties instead of class instances - this providing similar functionality while letting you instantiate via the typical var = new class() form.

  6. #6
    Senior Member
    Join Date
    Jun 2001
    Posts
    133
    That answers my question thanks everyone. Thank you too to Rosie, it was a good alternative example. That was a semicolon smile next to my thanks to you earlier that was turned automatically into a yawn smilie graphic which was not my intent, and may have made my thanks look insincere which it wasn't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center