4 Replies Latest reply on Sep 18, 2006 7:16 PM by NSurveyor

    Functions and  Variable Scope

    Lifeasalounge
      I am using this code:

      var getdata:LoadVars = new LoadVars();

      getdata.load("datatoflash.php");
      getdata.onLoad = function() {
      var graphData:Array = new Array(this.rating1, this.rating2, this.rating3, this.rating4, this.rating5);
      };
      trace(graphData)

      I understand variable scope. But i want to check something i have learnt. When the trace is performed, it has the arraye listed as undefined. This is because it was declared inside the function. Is there anyway of getting the array out of the function and using them else where in my movie? Even though the array is populated inside the function?
        • 1. Re: Functions and  Variable Scope
          kglad Adobe Community Professional & MVP
          yes, don't prefix your graphData line with "var" (which explicitly makes graphData local to your function and undefined outside your function).

          also, define your onLoad handler BEFORE you execute your getdata.load() statement.
          • 2. Re: Functions and  Variable Scope
            Lifeasalounge Level 1
            This is what i have now and the array is still showing up as undefined:

            var getdata:LoadVars = new LoadVars();

            getdata.onLoad = function() {
            graphData = new Array(this.rating1, this.rating2, this.rating3, this.rating4, this.rating5);
            }

            getdata.load("datatoflash.php");
            trace(graphData);

            Any ideas??
            • 3. Re: Functions and  Variable Scope
              Lifeasalounge Level 1
              This is what i have now and the array is still showing up as undefined:

              var getdata:LoadVars = new LoadVars();

              getdata.onLoad = function() {
              graphData = new Array(this.rating1, this.rating2, this.rating3, this.rating4, this.rating5);
              }

              getdata.load("datatoflash.php");
              trace(graphData);

              Any ideas??
              • 4. Re: Functions and  Variable Scope
                NSurveyor Level 2
                The trace is being called before the onLoad ever occurs as it takes time for a file to load. If you haven't ensured that the data has loaded at some part outside the onLoad, then it isn't a good idea to use it there. To demonstrate it is working, have:

                stop();
                var getdata:LoadVars = new LoadVars();
                getdata.onLoad = function() {
                graphData = new Array(this.rating1, this.rating2, this.rating3, this.rating4, this.rating5);
                nextFrame();
                }

                And on the next frame have: trace(graphData);