5 Replies Latest reply on Jul 20, 2007 7:15 PM by Marsoups

    Creating an instance of a movieclip class

    Marsoups Level 1
      Hi all,

      I'm wondering if somebody out there could answer this.. I've been struggling with this problem for the whole day now..

      I'm learning AS 3 now, to keep in touch with AS (which is evolving really fast !! Hopefully no major version 4 upgrades for at least another 10 years!!)

      Anyway, I have written a class that extends the movieclip class. Now , I want to be able to create instances on the stage of this new movieclip class, that has a special set of features -- in this case, it's a popup window class..

      I have realised that it is impossible to render the class (extension of movieclip) to stage when using the 'new' keyword. There are other ways around this I think by applying a class to a library symbol, or having this as the document class..

      However, I would like to be able to render movieclips with graphics in them (all generated with AS) onto the stage. Is it possible to do this with AS ?

      I was thinking maybe there would be a way
        • 1. Re: Creating an instance of a movieclip class
          Level 7
          Marsoups,

          > Anyway, I have written a class that extends the movieclip
          > class. Now , I want to be able to create instances on the
          > stage of this new movieclip class

          I'm with ya.

          > I have realised that it is impossible to render the class (extension
          > of movieclip) to stage when using the 'new' keyword.

          Really? In ActionScript 3.0, it is indeed possible to instantiate
          MovieClip with the "new" statement. Here's a quick sample:

          // AS file
          package {
          import flash.display.MovieClip;
          public class Test extends MovieClip {
          public function Test() {
          trace("test");
          }
          }
          }

          // In a keyframe of a FLA
          import Test;
          var t:Test = new Test();

          As expected, I get "test" in the Output panel. Since Test estends
          MovieClip, I can just as easily instantiate a new Test object with new
          Test().

          > However, I would like to be able to render movieclips with
          > graphics in them (all generated with AS) onto the stage. Is it
          > possible to do this with AS ?

          It's possible both in AS2 and AS3, as long as you publish to a version
          of Flash Player that supports the Bitmap and/or BitmapData classes (and
          related classes).


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Creating an instance of a movieclip class
            Marsoups Level 1
            Hi David,

            You are correct, it is possible to create an instance, but have you tried rendering anything to the screen?

            Taking the function you made, I'll add in some code...

            // AS file
            package {
            import flash.display.MovieClip;
            import flash.display.Shape;
            public class Test extends MovieClip {
            public function Test() {
            trace("test");
            var squareshape:Shape = new Shape();
            squareshape.graphics.beginFill(0x123456);
            squareshape.graphics.drawRect(0,0,300,300);
            squareshape.graphics.endFill();
            addChild(squareshape);
            }
            }
            }

            However hard I try, I can't get this to appear onto the stage... Have you tried to create a *visible* instance of the object on the *stage* itself ?

            Cheers,
            Daniel Hersch
            • 3. Re: Creating an instance of a movieclip class
              Rothrock Level 5
              So in the test fla you have:

              var test:Test=new Test();
              addChild(test);

              ??
              • 4. Re: Creating an instance of a movieclip class
                Level 7
                Daniel,

                > You are correct, it is possible to create an instance, but have
                > you tried rendering anything to the screen?

                Gotcha, gotcha. :) Now I see what you're saying. Rothrock nailed it,
                absolutely: lose the call to addChild() inside your class, and instead add
                your subclass instance to the display list after you instantiate it. Same
                as you would a base MovieClip instance.

                package {
                import flash.display.MovieClip;
                import flash.display.Shape;
                public class Test extends MovieClip {
                public function Test() {
                var squareshape:Shape = new Shape();
                squareshape.graphics.beginFill(0x123456);
                squareshape.graphics.drawRect(0,0,300,300);
                squareshape.graphics.endFill();
                }
                }
                }

                // in FLA (or from some other class) ...

                var test:Test = new Test();
                addChild(test);


                David Stiller
                Adobe Community Expert
                Dev blog, http://www.quip.net/blog/
                "Luck is the residue of good design."


                • 5. Re: Creating an instance of a movieclip class
                  Marsoups Level 1
                  Thanks guys for your help, much appreciated....

                  So much power in flash these days I tell ya.... Could almost write a web server out of AS 3.0 with these capabilities lol

                  cheers