4 Replies Latest reply on Sep 24, 2006 7:35 AM by Peter Lorent

    Vars within a subfunction in custom class...

    BRFlasher
      Hey hey!

      I have beeen a Flash coder for a long time, but just now I decided to step in custom classes, and I confess that there is some peculiar details that are driving me crazy. I am trying (with no success) since yesterday to figure out for what reason I cannot reach a class var from within a subfunction (it always return me undefined).

      Look below the simple example that shows that:


      code:
      --------------------------------------------------------------------------------


      class MyTest
      {
      private var testvar:Number;
      private var KeyboardListener:Object;

      function startVar ()
      {
      this["testvar"] = 1;
      }

      function startKeyboard ()
      {
      trace (this["testvar"]); // it works...
      KeyboardListener = new Object();
      KeyboardListener.onKeyDown = function ()
      {
      trace (this["testvar"]); // it DOESN'T works!
      }
      Key.addListener(KeyboardListener);
      }
      }

      --------------------------------------------------------------------------------

      As you can see, the first trace (that is within the function StartKeyboard works as is can see the var); but the second trace (tha is within the onKeyDown event subfunction DOESN'T works!!

      Well, the fact is that I NEED to have access to the vars within it, and I cant see a way to figure out this.

      Anybody out there already experienced this situation and know hot to resolve that?

      PS: Before someone there asks "Why this guy is using this["testvar"] instead testvar?" The answer is: that's because I will use vars with dynamic names, then I need to know how to make it work THIS way...

      Thanks a lot.
        • 1. Re: Vars within a subfunction in custom class...
          Peter Lorent Level 2
          You just need a local reference to access the class members.

          • 2. Re: Vars within a subfunction in custom class...
            Level 7
            I'm not sure wether it will be OK in your final design, nor do I really
            know wether it's proper OOP (probably not...) but this will work:

            //-------------- in class file:
            class MyTest
            {
            private var testvar:Number;

            public function MyTest(){
            startVar();
            startKeyboard();
            }

            private function startVar ()
            {
            testvar = 1;
            }

            private function startKeyboard ()
            {
            trace (testvar); // it works...
            Key.addListener(this);
            }

            public function onKeyDown(){
            trace (testvar)
            }

            }

            //-------------- in Flash timeline:
            import MyTest;
            t = new MyTest();

            Compared to your script, the main differences are that I ditched the
            KeyboardListener Object. The class itself will become the listener.
            Problem with your code was that when the onKeyDown eventhandler fired,
            it was in the context of the object 'KeyboardListener'. This object had
            no property (or variable) called 'testvar'. In other words: 'this'
            pointed towards the variable 'KeyboardListener' instead of the instance
            of class MyTest.
            If you would have written this:

            ...
            KeyboardListener = new Object();
            KeyboardListener.testvar = "I'm the property KeyboardListener";
            KeyboardListener.onKeyDown = function ()
            ...
            the output window would have shown: I'm the property KeyboardListener.

            I added a constructor. This is needed for the instantiation I used on
            the Flash timeline script.

            Next I removed the bracket access to the propertie (or variables) of 'this':

            trace(testvar);
            instead of:
            trace(this["testvar"]);

            I assume the first method is faster but that's no more than, I hope, an
            educated guess.
            ...
            Oh, read the rest of the post.... OK

            hope this helps.


            BRFlasher wrote:
            > Hey hey!
            >
            > I have beeen a Flash coder for a long time, but just now I decided to step in
            > custom classes, and I confess that there is some peculiar details that are
            > driving me crazy. I am trying (with no success) since yesterday to figure out
            > for what reason I cannot reach a class var from within a subfunction (it always
            > return me undefined).
            >
            > Look below the simple example that shows that:
            >
            >
            > code:
            >
            > --------------------------------------------------------------------------------
            >
            >
            > class MyTest
            > {
            > private var testvar:Number;
            > private var KeyboardListener:Object;
            >
            > function startVar ()
            > {
            > this["testvar"] = 1;
            > }
            >
            > function startKeyboard ()
            > {
            > trace (this["testvar"]); // it works...
            > KeyboardListener = new Object();
            > KeyboardListener.onKeyDown = function ()
            > {
            > trace (this["testvar"]); // it DOESN'T works!
            > }
            > Key.addListener(KeyboardListener);
            > }
            > }
            >
            >
            > --------------------------------------------------------------------------------
            >
            > As you can see, the first trace (that is within the function StartKeyboard
            > works as is can see the var); but the second trace (tha is within the onKeyDown
            > event subfunction DOESN'T works!!
            >
            > Well, the fact is that I NEED to have access to the vars within it, and I cant
            > see a way to figure out this.
            >
            > Anybody out there already experienced this situation and know hot to resolve
            > that?
            >
            > PS: Before someone there asks "Why this guy is using this["testvar"] instead
            > testvar?" The answer is: that's because I will use vars with dynamic names,
            > then I need to know how to make it work THIS way...
            >
            > Thanks a lot.
            >

            --
            aloft
            Hoge der A 20a
            9712AD Groningen
            The Netherlands
            info@aloft.nl
            http://www.aloft.nl
            tel: 06-22360652
            KVK: 02094451
            Bnk: 21.23.89.408
            • 3. Re: Vars within a subfunction in custom class...
              Level 7
              hehe :) saves typing compared to my sollution...

              LuigiL wrote:
              > You just need a local reference to access the class members.
              >
              >
              >
              > class MyTest
              > {
              > private var testvar:Number;
              > private var KeyboardListener:Object;
              >
              > function startVar ()
              > {
              > this["testvar"] = 1;
              > }
              >
              > function startKeyboard ()
              > {
              > //set a local reference to the current object (this class)
              > var thisObj:MyTest=this;
              > trace (this["testvar"]); // it works...
              > KeyboardListener = new Object();
              > KeyboardListener.onKeyDown = function ()
              > {
              > trace (thisObj["testvar"]); // it DOESN'T works!
              > }
              > Key.addListener(KeyboardListener);
              > }
              > }
              >

              --
              ----------
              Manno Bult
              manno@xs4all.nl
              • 4. Re: Vars within a subfunction in custom class...
                Peter Lorent Level 2
                That's a valid solution too but I wanted to show the 'basic' solution since this situation also pops up in other call back handlers. Another way to solve the problem is using the Delegate Class.