5 Replies Latest reply on May 29, 2007 8:24 PM by Rothrock

    Document Class

    KevPartner
      Actionscript 3/Flash 9 introduces the Document Class. I think I understand the concept that it enables you to link an external .as file which then acts as if it is the _root class. Firstly, is that correct? Secondly, given that you can create code in external .as files in ActionScript 2, what's the practical advantage of this new features over AS2?
      Kev
        • 1. Re: Document Class
          Level 7
          Kev,

          > Actionscript 3/Flash 9 introduces the Document Class.

          Correct.

          > I think I understand the concept that it enables you to
          > link an external .as file which then acts as if it is the _root
          > class. Firstly, is that correct?

          I suppose that's one way to put it. As I understand it -- and my
          strength is definitely with AS2 (still learning AS3) -- the document class
          must extend MovieClip, which makes sense, because the compiled SWF itself
          *is* a MovieClip instance. In terms of other languages, this is your "main"
          class, the entry point into the application.

          > Secondly, given that you can create code in external .as
          > files in ActionScript 2, what's the practical advantage of
          > this new features over AS2?

          AS3 is a completely different beast. Flash Player 9 has a brand new
          virtual machine, in fact, to run AS3 code (there are now two ActionScript
          Virtual Machines [AVM] in the Flash Player, for backward compatibility). To
          my thinking, the existance of the document class isn't the focus, here. The
          advantages to AS3 are numerous ... package declarations, for better
          organization; much improved event model; incredible performance increase;
          strongly typed variables at runtime; new datatypes ... it goes on. See this
          article for details.

          http://www.adobe.com/devnet/actionscript/articles/actionscript3_overview.html

          The document class basically allows you to keep every last shred of
          ActionScript outside your FLA, if you feel like it; whereas in AS2, you
          still have to put at least one line of code in frame 1 of the main timeline.


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


          • 2. Re: Document Class
            KevPartner Level 1
            Thanks David. I understand the other benefits to AS3, it was just this one that stumped me. Every explanation of the benefits of the Document Class that I found (and it is the first thing talked about in the official tutorial I've worked through) could equally apply to AS2 except for that one line #include statement. In practical terms, then, it's hard to see what the benefit is of that particular feature.

            I certainly like the fact that AS3 is more in line with other development tools: I can see that it would be much easier for a C# developer, for example, to make the move across. However, I am not relishing the task of translating my existing work into AS3. I know it will have to be done at some time but at the moment I'm trying to work out how urgent that's likely to be . For me, the speed increase is likely to be the clincher.

            Thanks for your help.
            Kev
            • 3. Re: Document Class
              Level 7
              Kev,

              > ... could equally apply to AS2 except for that one line
              > #include statement. In practical terms, then, it's hard to
              > see what the benefit is of that particular feature.

              It would be an import statement (if necessary) and then an instantiation
              of your AS2 class, rather than #include, but I agree, the document class
              assignment, on its own, isn't a stand-out feature.

              > I certainly like the fact that AS3 is more in line with
              > other development tools: I can see that it would be
              > much easier for a C# developer,

              Agreed. I do wonder what will become of the thousands upon thousands of
              developers for whom the current AS2 model "works just fine". I'm curious,
              for example, if we'll see a distinction between those who prefer AS1/AS2 and
              those who prefer AS3 -- somewhat akin to Visual Basic.NET devs "versus" C#
              devs. Time will tell. Exciting times. ;)

              > I am not relishing the task of translating my existing work
              > into AS3. I know it will have to be done at some time ...

              Ehh, maybe. Function libraries, sure. But old FLAs will continue to
              compile to AS2 just fine and, in theory, run the same in Flash Player 9 and
              above.


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


              • 4. Re: Document Class
                icarus786
                OK, I've been sold on the idea of separating my code from my movie. But how do I use it properly?

                Let's say I have a movie with some key frames (or screens). Each key frame has some components (e.g. text fields, buttons, etc). I have selected the declare stage variables in my publish settings so all variables are declared. BUT, I notice that in my document class constructor the names are accessible but only components in frame1 are actually available (the others are null). I was hoping to add event handlers in my constructor, but it appears that I can't for all the components that are not on frame1.

                This sort of makes sense as the movie has not reached the other key frames. So how does one program all these other components without adding code to the movie itself. Should I use a enter frame event handler and then initialize per key frame (a bit cumbersome) or is there some better way, I do notice that the variables are still not really available after a call to gotoAndPlay, they appear to exist some time after this call.

                I am newbie to flash so I don't know whether this is default functionality but I notice that when switching between key frames using my document class the components lose their values and need to be reinitialized. Is this correct behaviour or am I coding it incorrectly.

                It would be nice if somebody in Adobe created a proper explanation of Document Classes and some best practices on how to use them. I didn't find much info on the doucment class in adobe docs, but on the internet there was tutorial that explained how document classes merged with movie clip code by the compiler which was very interesting.

                Regards.
                • 5. Re: Document Class
                  Rothrock Level 5
                  Actually the document class can extend either MovieClip or Sprite. All the examples I'm working with in Keith Peters animation book extend Sprite.

                  According to him, if you are going to use the document class then you should have ABSOLUTELY no code in your FLA – not even comments. I haven't tested whether that continues for assets that you have in the library and export.

                  That is one advantage of using the Flash IDE is that if you have assets that you used to export for attachMovie and such you now export them as their own classes. So I would think there is nothing inappropriate for including their own class appropriate code?

                  I'm totally new to this as well – and I know far less than David – but I'm kind of grooving on this whole document class thing. At least so far as the examples I've been working through. We shall see how it plays out once I really start developing some of my own stuff.