1 Reply Latest reply on Jan 24, 2010 3:12 PM by Ned Murphy

    AS 3.0: variable scope issue - adding numbers


      I have event listener methods that obtain values, so each method will contain a variable: var number:int, number = 10;

      I want to add a global variable that adds these values,e.g.: var sum:int = 0; sum = number + number2....


      However when I attempt this sum is always 0.  The variables 'number(s)' have values only in their local methods.

      What do I need to do so that I can add these method values to this global variable?


      This is what I have:


      var sum: int = 0;

      function something(

      var number: int = 0;

      number = 10;

      sum = number;


      function something2(

      var number2:int = 0;

      number2 = 45;

      sum += number2;


      trace ("sum is: " + sum);//sum remains 0

        • 1. Re: AS 3.0: variable scope issue - adding numbers
          Ned Murphy Adobe Community Professional & MVP

          If that's what you actually have, you should only be getting error messages.  If that's not what you really have, showing what you really have will be better.


          Conceptually it could be correct, except that last line of code will trace out right after the sum value of 0 is assigned up top.  The functions (which are suffering terribly as valid code)  have to be called before they have any effect.  You could just have well have written...


          var sum: int = 0;

          trace ("sum is: " + sum);//sum remains 0