12 Replies Latest reply on Jun 14, 2006 12:36 PM by Peter Lorent

    Functions in AS 2.0

      Hi all

      Here's what I don't get. In AS 1, functions are a simple thing. My standard way of working is if there's a lot of functions in a piece, I keep all of those functions within 1 frame script, and then those get referenced throughout. Very simple.

      But now that I'm getting into AS 2, the one thing I don't get is when to create classes and how to reference functions within them.

      So say in AS 1, maybe I have 20 functions written in one frame script. But in AS 2, would I instead have 20 classes for each function?

      Or do I write one class for some object and any function that references that object gets written into that class?

      Or is it a combination of both? I think the real problem is that I don't fully understand OOP so I'm not fully understanding this yet.

      But if anyone could point me in the right direction, that would be great.

        • 1. Re: Functions in AS 2.0
          neil manuell Level 1
          a class is a group of methods (functions) and properties (variables) that do a specific function.

          like a tv

          it has input via its ariel (cable, stalite whatever)
          its buttons and power supply

          all its workings are on the inside of the box and you don't need to know about them

          its out put is audio (the sound) and visual (the picture)

          it doesn't may toast
          or wash your dishes

          Essential ActionScript 2.0
          By Colin Moock

          • 2. Re: Functions in AS 2.0
            ChrisFlynn Level 1
            OK so say I have a class called "tv"...any function that pertains to the tv is written within the tv class.

            Now I have another class called "dvd", which is its own thing, but I obviously want its functions to effect the tv class. Is that where I start using terms like "extends" or "interface" ?

            Thanks for the help.
            • 3. Re: Functions in AS 2.0
              neil manuell Level 1
              ok extends is more like this:

              class container

              class cup extends container

              in other words cup inherits everything from container and adds its own (which can overwrite the super class)

              interface is more like a class template
              • 4. Re: Functions in AS 2.0
                neil manuell Level 1
                if you want classes interact, you would either use data bindings or create event listners and write control code (external to the classes) that 'binds' them together
                • 5. Re: Functions in AS 2.0
                  Peter Lorent Level 2
                  >>(external to the classes)
                  Not neccesarily. Basically, the base class - tv - should provide the basic functionality: the screen. Extend it with a remote control or a dvd player. Then you can write a 'wrapper' class - homecinema - that controls how the classes interact. For example by using the EventDispatcher class or by implementing a design pattern like the Delegation Event Model. And... there is no rule that you have to use inheritance. You can also use seperate classes. It all depends on the job and your own preferences.
                  An interface is used to force classes to have the methods the interface describes.
                  • 6. Re: Functions in AS 2.0
                    ChrisFlynn Level 1
                    I actually like the idea of the wrapper class, that looks like it will work with my concept I have now.

                    Thanks for the help.
                    • 7. Re: Functions in AS 2.0
                      neil manuell Level 1
                      as you say the right tool for the job
                      I don't tend to use wrapper classes, as I general am creating applications that are constantly changing their functionality and user interfaces.
                      Wrapper Classes I find are too perminant
                      external control code allows me a more fluid and rapid development.
                      • 8. Re: Functions in AS 2.0
                        Not to get too OOPish, but Remote or DVD wouldn't extend TV. Remote would more than likely be an interface and DVDs (as well as DVDPlayer) would be separate objects. DVDs might implement a Media interface, but they themselves are actual objects. They might even extend from class PlasticDooDad, if you're feeling really ornery.

                        class TV implements RemoteReceiver
                        // Functions inherited from RemoteReceiver interface:
                        function setVolume(decibels:Number):Void;
                        function setInput(signal:InputSource):Void;
                        function setChannel(channel:Number):Void;
                        function setPower(onOrOff:Boolean):Void;

                        // Original functions:
                        function showImage(target:MovieClip):Void;
                        function playAudio(Void):Void;
                        } // ends class TV

                        "InputSource" might be an interface that Antenna, Satellite, DVDPlayer, and Karaoke all implement. In this manner, you could tell your TV to change inputs to the dvd player with the statement:

                        or to channel 7 from the antenna feed with these statements:

                        Home entertainment systems are a pretty good example for OOP since the backs of them have clearly labelled inputs and outputs. The output from your VCR has to go to the input of your TV. Plug it in anywhere else and all you get is snow.
                        • 9. Re: Functions in AS 2.0
                          neil manuell Level 1
                          >Plug it in anywhere else and all you get is snow.

                          or a very nasty shock ;-}
                          • 10. Re: Functions in AS 2.0
                            Peter Lorent Level 2
                            >>but Remote or DVD wouldn't extend TV
                            Possible if that is what you want/need. But, there is no law on how to tackle a specific job. There would be several feasible setups.
                            >>Functions inherited from RemoteReceiver interface
                            That's not possible. An interface can inherit from another interface. Classes that implement the interface agree to provide the methods that are described in the interface but they do not inherit the methods.
                            • 11. Re: Functions in AS 2.0
                              JustStartingOut Level 1
                              Unless your remote has a little LCD in it, it wouldn't be subclassing TV.

                              As for the interface implementation, notice that I put the function abstracts in the TV class. The RemoteReceiver interface declared them, and the TV class inherited the abstracts, and needs to give them definitions. What's so hard about understanding that?
                              • 12. Re: Functions in AS 2.0
                                Peter Lorent Level 2
                                >>What's so hard about understanding that?
                                Very hard. But let's say I have a pea for a brain.