2 Replies Latest reply on Jul 5, 2007 12:59 PM by MaxManNH

    Variables in Instanced Class

    MaxManNH Level 2
      I am working in AS 2.0 and I am having a problem making variables globally visible throughout my class.

      For example

      class Foo extends MovieClip
      {
      private var _seeAllFlag:Boolean
      private var _someNumber:Number
      public function Foo()
      {
      _seeAllFlag = false;
      _someNumber = 10;
      this.onPress = checkFlag;
      }

      function checkFlag()
      {
      trace(_seeAllFlag);
      trace(_someNumber);
      _someNumber = setInterval(checkInterval, 1000);
      }

      function checkInterval()
      {
      trace(_someNumber);
      }

      }
      When I trace the _someNumber in the checkInterval function it comes up undefined. But when I trace it in the constructor it comes up 10. I want to be able to access and change that particular variable from anywhere in the class. Is that not possible? Or is it some kind of scope issue with the setInterval function calling the class function? If that is the issue is there some way around that?
        • 1. Variables in Instanced Class
          MaxManNH Level 2
          Found the code that doesn't work. Posted above.
          • 2. Re: Variables in Instanced Class
            MaxManNH Level 2
            I figured it out I think.

            I changed the setInterval call to read:
            _someNumber = setInterval(checkInterval, 1000, this);

            Then I had the checkInterval function accept an object arguement:

            function checkInterval(obj:Object)

            Then I accessed the _someNumber variable via the object argument:
            trace(obj._someNumber);

            Now I am getting the right trace. Can anyone tell me if this is the only way to do it or is there some better way to provide scope to the setInterval function when it is called in a class?