3 Replies Latest reply on Aug 25, 2009 9:46 PM by BexKelleher

    xml.onLoad variables undefined outside function

    BexKelleher

      Hi all,

      I've been searching this problem for a while and have tried a number of solutions found on google but nothing has worked so far. I'm sure this is a common problem, but due to my lack of flash knowledge I can't logically work through it as I'm used to windows form development, it makes sense to me that the function could access the variables I have defined but they dissapear after the .onLoad event. Does anyone know the common solution to this problem? I want to be able to pass the xml details into the local variables so I can pass on the details into a dynamic text field when movies are hovered over etc.

      Thanks in advance for any help.

       

       

      var myXML:XML = new XML();

      myXML.ignoreWhite=true

       

      var nav1TitleText:String;

      var nav1Text:String;

      var nav2TitleText:String;

      var nav2Text:String;

      var nav3TitleText:String;

      var nav3Text:String;

      var nav4TitleText:String;

      var nav4Text:String;

       

      myXML.load("navigation.xml");

       

      myXML.onLoad = function(success){

      if (success){

      nav1TitleText = myXML.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;

      trace(nav1TitleText); \\ This one shows the text from the xml file

      nav1Text = myXML.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue;

      nav2TitleText = myXML.firstChild.childNodes[1].childNodes[1].firstChild.nodeValue;

      nav2Text = myXML.firstChild.childNodes[1].childNodes[2].firstChild.nodeValue;

      nav3TitleText = myXML.firstChild.childNodes[2].childNodes[1].firstChild.nodeValue;

      nav3Text = myXML.firstChild.childNodes[2].childNodes[2].firstChild.nodeValue;

      nav4TitleText = myXML.firstChild.childNodes[3].childNodes[1].firstChild.nodeValue;

      nav4Text = myXML.firstChild.childNodes[3].childNodes[2].firstChild.nodeValue;

      }

      }

       

      trace(nav1TitleText);  // This one traces undefined

        • 1. Re: xml.onLoad variables undefined outside function
          webqaflash Level 3

          I think it is beacause of script running problem.

           

           

          var myXML:XML = new XML();

          myXML.ignoreWhite=true

           

          var nav1TitleText:String;

          var nav1Text:String;

          var nav2TitleText:String;

          var nav2Text:String;

          var nav3TitleText:String;

          var nav3Text:String;

          var nav4TitleText:String;

          var nav4Text:String;

           

          myXML.load("navigation.xml");

           

          myXML.onLoad = function(success){

          if (success){

          nav1TitleText = myXML.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;

          trace(nav1TitleText); \\ This one shows the text from the xml file

          nav1Text = myXML.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue;

          nav2TitleText = myXML.firstChild.childNodes[1].childNodes[1].firstChild.nodeValue;

          nav2Text = myXML.firstChild.childNodes[1].childNodes[2].firstChild.nodeValue;

          nav3TitleText = myXML.firstChild.childNodes[2].childNodes[1].firstChild.nodeValue;

          nav3Text = myXML.firstChild.childNodes[2].childNodes[2].firstChild.nodeValue;

          nav4TitleText = myXML.firstChild.childNodes[3].childNodes[1].firstChild.nodeValue;

          nav4Text = myXML.firstChild.childNodes[3].childNodes[2].firstChild.nodeValue;

          loadedValues()

          }

          }

           

          trace(nav1TitleText);  // This one traces undefined--------THIS WILL EXECUTING BEFORE THE XML IS LOADED

           

          //Change this line with the below

           

          function loadedValues(){

               trace(nav1TitleText);

          }

          • 2. Re: xml.onLoad variables undefined outside function
            BexKelleher Level 1

            Thanks for your help, I have got it working now by doing as you suggested. In .net programming when you specify something to load, it will run the onload first, and then continue through to the rest of the code. I'm assuming flash runs all of the code (non-functions) regardless of order of code. So it was running the trace below the onLoad before the onLoad, even though I told it to myXML.Load() before the other code...

             

            Seriously doesn't make sense to me.. lol

            var myXML:XML = new XML();

            myXML.ignoreWhite=true

             

            var nav1TitleText:String;

            var nav1Text:String;

            var nav2TitleText:String;

            var nav2Text:String;

            var nav3TitleText:String;

            var nav3Text:String;

            var nav4TitleText:String;

            var nav4Text:String;

             

            myXML.load("navigation.xml");

             

            myXML.onLoad = function(success){

            if (success){

            nav1TitleText = myXML.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;

            trace(nav1TitleText);

            nav1Text = myXML.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue;

            nav2TitleText = myXML.firstChild.childNodes[1].childNodes[1].firstChild.nodeValue;

            nav2Text = myXML.firstChild.childNodes[1].childNodes[2].firstChild.nodeValue;

            nav3TitleText = myXML.firstChild.childNodes[2].childNodes[1].firstChild.nodeValue;

            nav3Text = myXML.firstChild.childNodes[2].childNodes[2].firstChild.nodeValue;

            nav4TitleText = myXML.firstChild.childNodes[3].childNodes[1].firstChild.nodeValue;

            nav4Text = myXML.firstChild.childNodes[3].childNodes[2].firstChild.nodeValue;

            loadedValues();

            }

            }

             

            function loadedValues()

            {

            trace(nav1TitleText);

            navItem1.title1.text = nav1TitleText;

            navItem2.title2.text = nav2TitleText;

            navItem3.title3.text = nav3TitleText;

            navItem4.title4.text = nav4TitleText;

            }


            Thank you again for your reply!

            Bex