4 Replies Latest reply on Mar 31, 2011 4:31 AM by Ned Murphy

    Variable Out of Scope problem

    varunsud

      Below is the code snippet of my function.

      It reads values from the XML file and get the URL of an Image file e.g. Abc.jpg.

      On reading the URL, i create an empty movieclip to further create animations. But now my problem is that i cannot read one of the variables defined to store XML values since it is not in scope. I have been trying to look for a workaround and haven't succeeded as yet. Little help ?

       

      private function init():Void {

       

      var polaroid_image:String;

      var myXML:XML = new XML();

      myXML.ignoreWhite=true;

      myXML.load("content.xml");

      var myImage = myXML.firstChild.childNodes;

      myXML.onLoad = function(success) {

      if (success) {

      myImage = myXML.firstChild.childNodes;

      polaroid_image = myImage[1].firstChild.childNodes;

      trace(polaroid_image);

      }

      }

       

      trace(polaroid_image);  // PROBLEM HERE : outputs 'undefined'

      this._border = 10;

      this.initSize();

      this.setPosition(48,35);

      this.createBackground();

       

      var _mcPolaroid:MovieClip = this._mcBg.createEmptyMovieClip("polaroid_mc", this._mcBg.getNextHighestDepth());

      _mcPolaroid.loadMovie(polaroid_image, "polaroid_mc"); // PROBLEM HERE : since the value is 'undefined', it can't be used.

       

       

      _mcPolaroid._x = _border;

      _mcPolaroid._y = _border;

       

       

      var _rmcPolaroid:MovieClip = this._rmcBg.createEmptyMovieClip("rpolaroid_mc", this._rmcBg.getNextHighestDepth());

       

       

      _rmcPolaroid._yscale *=-1;

      _rmcPolaroid._y = _rmcPolaroid._y + _h + _border ;

      _rmcPolaroid._x =_border;

       

      }

        • 1. Re: Variable Out of Scope problem
          Ned Murphy Adobe Community Professional & MVP

          Try tracing myImage since it defines polaroid_image.  I don't think you have a value defined for it.  Especially not at this point since you just initiate loading and then try to assign it a value relative to the file that has not yet loaded...

           

          myXML.load("content.xml");

          var myImage = myXML.firstChild.childNodes;

          1 person found this helpful
          • 2. Re: Variable Out of Scope problem
            logic interface Level 1

            You already know the problem... a variable out of scope problem.

            So all you need to do is declare the "var polaroid_image:String;" outside the private init function.

             

             

            EDIT: my bad, to quick to respond.

            Your onload function will only be executed once the XML is loaded.

            You should wait untill the XML is loaded, before running anything else.

            That's why it'll return values as undefined, because they aren't loaded yet.

            Put those actions into a seperate function, and call that function from within the onload statement.

            • 3. Re: Variable Out of Scope problem
              varunsud Level 1

              done that, it outputs correct values. maybe its a 'out of scope' problem whcih i m now trying to resolve

              • 4. Re: Variable Out of Scope problem
                Ned Murphy Adobe Community Professional & MVP

                It is not a scope issue.  It is due to you trying to deal with a value that is not yet defined.  Just because you place that code after the onload function does not mean it executes after the onload function.  The onload function sits and waits for the loading to finish, but the rest of the code after it executes before the loading is done.

                 

                You need to have any processing that is supposed to happen after loading is complete within the onload function.

                 

                You can confirm it is not a scope issue by assigning a value to polaroid_image when it is declared... you should see that value when you run it...

                 

                var polaroid_image:String = "value before file is loaded";