15 Replies Latest reply on Jul 27, 2007 6:50 AM by Newsgroup_User

    AS 3.0 Class question...

    C-Rock Level 2
      I've created a class in flash. I don't want to make the document class this class because then I need to import the other classes into this class that I'm using in my movie. I use a dummy clip to bring the class into my movie.

      On a button's function located in the fla, how do I call a function that is in that class? Or am I doing something wrong?

      I have a class called "TestClass" and a function in it called init(); I just want to call init() from my main fla timeline, why doesn't this work?

      init();
      or
      TestClass.init();
      ???

      Thanks,
        • 1. AS 3.0 Class question...
          Greg Dove Level 4
          Do you mean like this

          var thisSaveJpeg:TestClass= new TestClass();
          thisSaveJpeg.init(); // if this is always required you could do it inside the class' constructor and avoid the need to call init each time
          thisSaveJpeg.saveJpeg(imageData);


          Or if you just want to call an init() method for the class, you could declare init as a static function inside your class.
          then you can use something like
          TestClass.init();

          Ooops I just realised its AS3. I haven't written any custom classes in as3 yet. I know there are some subtle differences. So don't know how static class members are declared - if its the same as as2 or not - but the general approach should be similar I guess

          -GWD
          • 2. Re: AS 3.0 Class question...
            kglad Adobe Community Professional & MVP
            that will work if TestClass is a public class in your swf's path and init() is a public static method of TestClass.
            • 3. Re: AS 3.0 Class question...
              C-Rock Level 2
              I think I figured it out. Just not really understanding how to make a repeated remoting call without calling functions in the class. I would like my remoting call to be in my main timeline, it's easier that way.

              I would like to just have the saveJpeg(imageDate) function(s) in my main fla. It won't work because it can't find the "rs" service and I don't know how to make it use the public rs variable....

              • 4. Re: AS 3.0 Class question...
                Greg Dove Level 4
                I'm not sure I understand the issue well.

                I think you need to import RemotingService inside RemotingTest:

                package {

                import flash.display.MovieClip;
                import flash.events.MouseEvent;
                import flash.net.Responder;
                import RemotingService;

                public class RemotingTest extends MovieClip{
                ..etc

                • 5. Re: AS 3.0 Class question...
                  Level 7
                  >>that will work if TestClass is a public class in your swf's path and
                  >>init() is a public static method of TestClass.

                  --
                  Dave -
                  Head Developer
                  http://www.blurredistinction.com
                  Adobe Community Expert
                  http://www.adobe.com/communities/experts/


                  • 6. Re: AS 3.0 Class question...
                    Level 7
                    >>that will work if TestClass is a public class in your swf's path and
                    >>init() is a public static method of TestClass.

                    Hate when I do that... I just wanted to clarify here - init would not have
                    to be static, since the class got instantiated with new. So it'd be an
                    instance method.

                    --
                    Dave -
                    Head Developer
                    http://www.blurredistinction.com
                    Adobe Community Expert
                    http://www.adobe.com/communities/experts/


                    • 7. Re: AS 3.0 Class question...
                      Greg Dove Level 4
                      Actually I was trying (and failed miserably) to give illustrative examples of both per instance and static methods.

                      • 8. Re: AS 3.0 Class question...
                        C-Rock Level 2
                        looks like i just need to learn a little more about call functions in a class.

                        i've always just made all my functions inside my main fla on a functions layer in the second frame.

                        i was trying to go to the next level in programming by using other classes and such and as 3.0.

                        i just don't understand how to make a simple call to a class function and how to have that function interact with the stage.

                        i can't even get a class function to write to a text box on the stage..... grrrr....

                        what are "static" functions all about? if i make my class functions static can i just make regular calls to them?

                        as 3.0 is harder but better i'm sure! grrr again....
                        • 9. Re: AS 3.0 Class question...
                          Greg Dove Level 4


                          "what are "static" functions all about? if i make my class functions static can i just make regular calls to them?"

                          The way I like to think about static methods is like comparing to something like the Math class.

                          You don't make instances of the Math class (e.g. you don't do var myMath:Math = new Math() ) - you just use Math.something().

                          Of course you can have a class with both static methods or properties - or constants - and instance-based properties and methods. I can't think of an example off-hand - but there are plenty of component classes where the class has static properties as constants. E.g. FLVPlayback.BUFFERING

                          I hope that makes some sense. Once you get to the "aha!" moment with OOP you start doing almost everything away from the timeline - because it just seems to make sense.

                          • 10. Re: AS 3.0 Class question...
                            Level 7
                            "Static" means that one copy is ever held in memory, and this is done in
                            the prototype class, not in any instance. That means that you don't need
                            to create an instance of the class in order to use the method or
                            property. This is a very effective memory management technique and can
                            be used for almost all methods (most, after all, don't need to be copied
                            to perform the same functions). There is an entire type of programming
                            called the Singleton pattern which advocates the use of static methods
                            and properties.

                            Patrick

                            GWD wrote:
                            > "what are "static" functions all about? if i make my class functions static
                            > can i just make regular calls to them?"
                            >
                            > The way I like to think about static methods is like comparing to something
                            > like the Math class.
                            >
                            > You don't make instances of the Math class (e.g. you don't do var myMath:Math
                            > = new Math() ) - you just use Math.something().
                            >
                            > Of course you can have a class with both static methods or properties - or
                            > constants - and instance-based properties and methods. I can't think of an
                            > example off-hand - but there are plenty of component classes where the class
                            > has static properties as constants. E.g. FLVPlayback.BUFFERING
                            >
                            > I hope that makes some sense. Once you get to the "aha!" moment with OOP you
                            > start doing almost everything away from the timeline - because it just seems to
                            > make sense.
                            >
                            >
                            >

                            --
                            http://www.baynewmedia.com
                            Faster, easier, better...ActionScript development taken to new heights.
                            Download the BNMAPI today. You'll wonder how you ever did without it!
                            Available for ActionScript 2.0/3.0.
                            • 11. Re: AS 3.0 Class question...
                              C-Rock Level 2
                              okay, thanks for your help guys.
                              • 12. Re: AS 3.0 Class question...
                                Level 7
                                Honestly, with a name like C-Rock, how could we resist?!

                                C-Rock wrote:
                                > okay, thanks for your help guys.

                                --
                                http://www.baynewmedia.com
                                Faster, easier, better...ActionScript development taken to new heights.
                                Download the BNMAPI today. You'll wonder how you ever did without it!
                                Available for ActionScript 2.0/3.0.
                                • 13. Re: AS 3.0 Class question...
                                  Greg Dove Level 4
                                  @ Patrick:

                                  "This is a very effective memory management technique and can
                                  be used for almost all methods (most, after all, don't need to be copied
                                  to perform the same functions). "

                                  > I thought the idea with instances is that the actual functions are not copied to the instances, but each instance's method call references a single definition of the method at the prototype level... is this correct or not - perhaps I don't understand things as well as I thought? - it wouldn't be the first time, lol
                                  {I do understand that even if my original assumption were true, maintaining each reference to the prototype occupies memory.}



                                  • 14. Re: AS 3.0 Class question...
                                    kglad Adobe Community Professional & MVP
                                    (if TestClass is the class name, TestClass.init(); will work if init() is a public static method.)
                                    • 15. Re: AS 3.0 Class question...
                                      Level 7
                                      >>(if TestClass is the class name, TestClass.init(); will work if init() is
                                      >>a public static method.)

                                      I see what you meant, right... <g>

                                      --
                                      Dave -
                                      Head Developer
                                      http://www.blurredistinction.com
                                      Adobe Community Expert
                                      http://www.adobe.com/communities/experts/