7 Replies Latest reply on Nov 11, 2007 9:29 AM by kglad

    just a query regarding a simple class definition

    avonova Level 1
      hi folks,

      can anyone offer some clarification on the following script item please? the comments are mine but the script is extracted from a study exercise/example in an ebook publication by 'Friends of ED'.

      here's the script with my comments:-

      class Vogon {
      public var myColour:Number; //. . . . . . . . . . . . . . . .defines an EXPLICIT property of the Vogon class

      public function Vogon() {} // . . . . . . . . . . . . . . . . . . . defines an 'empty' Constructor Function

      public function applyNewColour():Void{ // . . . . . . defines a method of the Vogon class that facilitates changes of object colour
      var objColour:Color = new Color(this); // . . . . . . . create a local variable titled objColor
      objColour.setRGB(myColour);
      }
      }

      and here's my query:-

      can you offer a summary comment that describes (kind of) what's going on in the last line and/or how it 'ties in' with the use of (this).

      I accept that the use of (this) is reflexive on the object making use of it BUT I feel I don't fully grasp what is going on when (this) is being used within this type of context - sorry, I can be a dim when it comes to this kind of scripting and its associated concepts.

      any help is greatly appreciated.

      best wishes,

      avonova Text
        • 1. Re: just a query regarding a simple class definition
          kglad Adobe Community Professional & MVP
          i don't think this is great programming.

          first, nothing is going to happen when the applyNewColour() method is called unless myColour is explicitly defined.

          second, the use of "this" in applyNewColour serves no purpose except to make the code more difficult to understand. better coding would be:

          • 2. Re: just a query regarding a simple class definition
            avonova Level 1
            hi kglad,

            many thanks for your post.

            regarding the programming, I think the study material is leading me, in a stepwise manner, to good programming practises by showing, in part, not so good practises; the study material makes reference to the function applyNewColour as 'cumbersome'.

            I do understand that the function myColour needs to be explicitly defined for anything meaningful to happen, however thank you for confirming that.

            What the study material has bought to a head for me is that I have not fully digested and understood how the keyword 'this' is used or when it is appropriate to use it or how it is correctly used.

            Perhaps it's because I have not sufficently mastered other aspects and principles of actionscript that I have a block with the keyword 'this'?

            If you are able to offer any insight on the 'this' item it will be gratefully received with thanks, if not, many thanks for getting me thus far with my efforts.

            Either way I shall continue with my study material in the hope that the proverbial penny will eventually drop for me.

            best wishes,

            avonova
            • 3. Re: just a query regarding a simple class definition
              kglad Adobe Community Professional & MVP
              hey, i'm very comfortable with actionscript and i can run into problems with "scope" in class files. that's why i like to explicitly assign a variable to be a class instance. eg, _vogon.

              it's very easy to misuse "this" in a class. all you need is a data handler (like a loadvars instance or a moviecliploader instance) or a setInterval() loop and "this" no longer refers to a class instance.

              when in doubt, it's very handy to use

              trace(this);

              // and sometimes

              this(this instanceof Vogon); // or whatever class if trace(this) results in an object
              • 4. Re: just a query regarding a simple class definition
                avonova Level 1
                thank you for your feedback kglad,

                i think your post has kinda reassured me that I'm not mentally struggling with a 'simple' issue here yet also kind of unsettled me in that i'm now wondering if i'll ever get a handle on it - if you know what i mean. i suspect, for me, it'll be one of those aspects of flash that will have an enduring elusive quality to it - and just holding that thought will help me to perservere with it.

                anyway, what i do pick up from your last post, and shall try to observe and practise in my coding, is to explicitly assign a variable to be a class instance (as appropriate) eg, _vogon so as to better define variable 'scope' within class files which, i believe, will get me another step closer to best practice.

                this post, and your contributions today, has certainly helped my actionscript study efforts and, while not definitively answering the query, allows me to broaden my understanding a little further and move forward.

                again, thank you.
                • 5. Re: just a query regarding a simple class definition
                  kglad Adobe Community Professional & MVP
                  you're welcome.

                  to more explicitly answer your question: "this" refers to a Vogon instance. when the applyNewColour() method is applied to that instance, its color will be changed (if myColour is appropriately defined).
                  • 6. Re: just a query regarding a simple class definition
                    avonova Level 1
                    sorted! - well, at least for now.

                    thank you.