3 Replies Latest reply on Oct 11, 2007 8:56 AM by Newsgroup_User

    Actionscript 3.0 newbie question

    martincou Level 3
      Hi everyone,

      I just started to learn AS 3.0. I was ok with 2.0 but now I read a lot of things out there that encourage people to make THE move! I am one of those… but I’m a newbie… so, I have a question: I just read a 2.0 book that said that Top-Level classes are classes that you can use without the need to create an instance. Examples of this type of class include the Math, Mouse, and Key classes. When you think about it, top-level classes make sense. Is there ever really a need to have more than one instance of the Mouse class or the Math class? With the Math class, you simply pass a number into a method, and a result is returned. So why in 3.0, when I use the flyout menu in the action panel, the first choice is «Top-Level» and I see Array and String out there? It doesn’t make sense to me because when you create an Array or String using this syntax — var myArray:Array = new Array(); — you create an instance of the Array class… Can someone explain that to me why they sort the new action panel’s flyout like it is in Flash CS3?

      Thanks a lot for your help!

      ---
      Martin
      http://www.martinc.biz
        • 1. Re: Actionscript 3.0 newbie question
          Level 7
          Martin,

          > I just read a 2.0 book that said that Top-Level classes are
          > classes that you can use without the need to create an instance.
          > Examples of this type of class include the Math, Mouse, and
          > Key classes.

          I haven't heard of such classes being described as "top level" classes,
          but Math, Mouse, and Key are examples of something called static classes.
          Some classes have both static and non-static members -- JavaScript's RegExp
          comes to mind -- but with something like Math, every member (every property
          and method) is static, which means you never need an instance of the Math
          class. As you've mentioned, an instance wouldn't even make sense. Math.PI
          is Math.PI, always.

          > So why in 3.0, when I use the flyout menu in the action panel,
          > the first choice is ?Top-Level? and I see Array and String out
          > there? It doesn?t make sense to me

          Aha, that's where you're seeing Top Level. Well, the mention of static
          should clear that up, right? Classes that appear in the Top Level menu are
          not necessarily static, though some might be. Different concept. What Top
          Level means, in this context, is that these are classes that aren't
          organized into a package. They can be referenced without an import
          statement.

          In AS2, for example, the BitmapData class is located in a package
          (packages are really just hierarchical arrangements). In order to use
          BitmapData in Flasy 8, you have to put "import flash.display.BitmapData" in
          your code. AS3 is much more highly organized around packages, but some
          classes are so often used, they can be referenced simply by name.

          As a side note, the import statement is required in AS3 code that is
          stored in external text files -- that is, in classes -- but AS3 timeline
          code often allows you to omit importing a class's package.

          > when you create an Array or String using this syntax ?
          > var myArray:Array = new Array(); ? you create an instance
          > of the Array class?

          Yes, that creates an instance of Array. You can use new Array() or the
          shorcut [] ...

          var myArray:Array = [];

          Strings can be instantiated with new String(), but most people use the
          shorthand "" ...

          var myString:String = "some string";

          Not all classes support shortcuts like that, but the most often ones do.
          Instances of Object, for example, can be created with new Object() or {} ...

          var myObject:Object = {};


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: Actionscript 3.0 newbie question
            martincou Level 3
            Thanks a lot for your help! You teached me a lot of new things to optimize my learning of AS 3.0. For the moment, I have a last one question: In the top-level classes, in the action panel (again!), why some constructor words (like Array()) appears twice, one above of the other? Is it a bug or an error? I'm on XP SP 2. Thanks (again) for your answer!
            • 3. Re: Actionscript 3.0 newbie question
              Level 7
              Martin,

              > why some constructor words (like Array()) appears twice,
              > one above of the other? Is it a bug or an error?

              Looks like an error to me. ;) Good catch! As it turns out, I prefer
              typing -- to me, it's faster -- so I haven't especially browsed through the
              upper left corner of the Actions panel.


              David Stiller
              Contributor, How to Cheat in Flash CS3
              http://tinyurl.com/2cp6na
              "Luck is the residue of good design."