16 Replies Latest reply on Oct 5, 2008 9:04 PM by WIDS-Chris

    Accessing a VAR from within a Function

    WIDS-Chris
      I'm sure this is a simple question, but I have been batteling it all night. I am pulling data from a database, this works fine. the data loads into flash fine, but I cant figure out how to use this data outside of the function.

      I have looked at global var, timeline vars, but I must be missing something. I have attached my code, if you run it, it will load the data, I left the link live.

      In the function the data traces correctly, outside of the function it fails.

      Thanks
      ~Chris
        • 1. Re: Accessing a VAR from within a Function
          Devendran Level 1
          The scope of the variable you used is with in that function only.

          to use the Data Declare the Variable Externaly.

          and call the Execution function After the XML Loaded.

          XML Loading will take some delay to load the XML. So to use the Data, call the using Function From the onLoad.
          • 2. Re: Accessing a VAR from within a Function
            WIDS-Chris Level 1
            Any chance you can give me an example, I thought I tried that (example:

            var myVar = "test";

            myFunction
            myVar = "works";
            }

            trace (myVar) // test


            I know the example above is not the correct code (I just made a quik example), but when I declare it outside of the function, then change it in the function, the results are only aval in the function, once I leave the function it changes back. an example would be great, Thanks
            • 3. Re: Accessing a VAR from within a Function
              Devendran Level 1
              The onLoad event and other load events will take some time to complete the loading, so we can't judge the delay. Depending upon the Bandwidth, Server Speed and other affecting factors the delay will be vary. So always better to use a seperate function call to implement the loaded data. For Example if you would like to build a list of Students taken from XML, the implementation can be like this

              • 4. Re: Accessing a VAR from within a Function
                WIDS-Chris Level 1
                Thanks, I will mess with it tonight, thanks
                • 5. Re: Accessing a VAR from within a Function
                  WIDS-Chris Level 1
                  Ok, I attempted this last night, and I am unning into an issue. Heres what I have, I am pulling data from a mySQL database, into a php file that includes XML, Here is the PHP Script.

                  //////////////////////////////////////////////////////////////////////////////////

                  $query = "SELECT * FROM Teams WHERE active = 'Y'";
                  $resultID = mysql_query($query, $linkID) or die("Data not found.");

                  $xml_output = "<?xml version=\"1.0\"?>\n";

                  $xml_output .= "<teamData>\n";

                  for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
                  $row = mysql_fetch_assoc($resultID);
                  $xml_output .= "\t<item>\n";

                  $xml_output .= "\t\t<teamName>" . $row['TeamName'] . "</teamName>\n";
                  $xml_output .= "\t\t<manager>" . $row['Manager'] . "</manager>\n";
                  $xml_output .= "\t\t<order>" . $row['LeagueOrder'] . "</order>\n";

                  $xml_output .= "\t</item>\n";
                  }

                  $xml_output .= "</teamData>";

                  echo $xml_output;

                  ?>

                  /////////////////////////////////////////////////////////////////////////////////////

                  NOTE: This script does run fine, if I run the PHP I do get the correct results.

                  so I modified the Action Script you sent me a little to fit my PHP script. but I must be missing something. I think the problem is, what part of my XML goes after "studentData ."

                  newTextField.text = studentData .teamName + " :: " + studentData.manager + " :: " + studentData .order;



                  Thanks Again for the help

                  /////////////////////////////////////////////////////////////////////////////////////
                  • 6. Re: Accessing a VAR from within a Function
                    Devendran Level 1
                    yes,
                    you have changed the XML Structure but you didn't changed the Parsing part. your out XML Will be like this.

                    <teamData>
                    <item>
                    <teamName>Some Team</teamName>
                    <manager>Some manager</manager>
                    <order>some Order</order>
                    </item>
                    <item>
                    <teamName>Some Team</teamName>
                    <manager>Some manager</manager>
                    <order>some Order</order>
                    </item>
                    </teamData>

                    in my XML i used everything in the Attribute. But you are using it as a child so you need to change the parsing

                    • 7. Re: Accessing a VAR from within a Function
                      WIDS-Chris Level 1
                      Thank you, that did produce results (Correct results). But now bringing back to my original question. Now that this data do populate in Flash, how can I use this data outside of the function.

                      I wanna load this data in my main movie, then several MC will load inside of this main movie, I want to be able to use this data a VAR in other movies (inside of the main MC).

                      Thanks Again. I have attached what my code looks like. (This first trace's work because it is inside the Function, the 2nd trace statements dont work because they are outside of the function.)
                      • 8. Re: Accessing a VAR from within a Function
                        Rothrock Level 5
                        Don't declare your dataArray inside the xmlLoader. By declaring it inside there you make it only accessible while the onLoad is running and then it is gone.

                        Of course there is also no reason why you would have to tear up the XML and put it into an array in any event. You could always just access the array information like such:

                        xmlLoader.firstChild.firstChild.childNodes[0].firstChild.nodeValue

                        Of course you can only do that after the xml has loaded.
                        • 9. Accessing a VAR from within a Function
                          Devendran Level 1
                          ["Of course there is also no reason why you would have to tear up the XML and put it into an array in any event"]

                          Yes of course you no need to tear up the XML, But When you are using the XML data more than one place change of a single attribute will let you to a head ache, You need to search the entire Code to change it. But parsing the XML and having it ready to use will reduce the correction time.

                          And this kind of practice will help you to join Multiple XML Data into a single Roof.


                          • 10. Re: Accessing a VAR from within a Function
                            WIDS-Chris Level 1
                            So how do I use this data?

                            I tried

                            trace (xmlLoader.firstChild.firstChild.childNodes[0].firstChild.nodeValue)

                            It didnt work

                            Thanks Guys
                            • 11. Re: Accessing a VAR from within a Function
                              Rothrock Level 5
                              Well if it works inside your xmlLoader -- where you are extracting that node value and pushing it into your array -- then it will work outside as well.

                              But you have to wait for the XML to finish loading first.

                              Are you sure that you are accessing your xml nodes properly? Using the code you have shown above, put this line just before the dataArray.push() line.

                              trace("The nodeValue is: "+i.childNodes[0].firstChild.nodeValue);

                              What do you get in the output window.

                              A good way to learn how to do this stuff is to put aside your actual current project and make a simpler easier test version. Make just a simple file with only a couple nodes and use the examples from the help files to try it out. Once you've got it down a bit then move on to something a bit more complex and finally your project will be much easier.
                              • 12. Re: Accessing a VAR from within a Function
                                WIDS-Chris Level 1
                                Thank you, and that is how I do it, I run a small part of the file.

                                When I run this before the: dataArray.push

                                It displays, but again it is inside of a function. I am looking to use this data globally I want to use this imported data in all MC that load in the main movie.


                                I don't know if I am missing something, but thanks for the help
                                • 13. Re: Accessing a VAR from within a Function
                                  Rothrock Level 5
                                  So how are you using it outside the onLoad function?

                                  Generally if I'm doing something with loading of external data I put that code on frame one and give frame 1 a label of init or something.

                                  Then on frame ten of the main timeline I put a label of start or something similar. And then code like this:

                                  var home:MovieClip=this;

                                  var myXML:XML=new XML();
                                  myXML.ignoreWhite=true;
                                  myXML.onLoad=function(){
                                  // do somestuff
                                  home.gotoAndStop("start");
                                  }
                                  myXML.load("myFile.xml");

                                  //

                                  Then on the start frame code such as:

                                  trace(this.myXML)

                                  should work.

                                  My guess is you aren't properly waiting for the xml to load completely before you try and reference the xml directly. And as devendran pointed out it might make more sense to make and array or not depending upon your situation.
                                  • 14. Re: Accessing a VAR from within a Function
                                    WIDS-Chris Level 1
                                    How long does it take for the xml data to load? Can I run that maybe on frame with the pre-loader, is one frame long enough? Sorry I havent really used XML.
                                    • 15. Re: Accessing a VAR from within a Function
                                      Rothrock Level 5
                                      Oh, I forgot to put a stop(); before the home variable.

                                      I only said frame 10 so that you would have enough frames to view the label. It makes it easier to visually see what is going on with your timelines. There is no way to guess how many frames it might take.

                                      There is no way to know how long the XML will take. It depends upon how big the file is, how fast the internet connection is, server load, and many, many other variables. That is why you use the onLoad function to say, in effect, "Okay, I'm ready now, let's move ahead and get this thing started."

                                      You will need to wait regardless of if you want to access the XML directly or use an array.

                                      I really recommend that you ignore your current project for a little bit and try few simple tests to understand how it works.
                                      • 16. Re: Accessing a VAR from within a Function
                                        WIDS-Chris Level 1
                                        Thank you both for the answers, a comonation of both of your input I was able to figure it out. What I was missing is that the function with the array wasnt displaying until the onLoad was done, and as you guys stated the trace outside of the function wasnt displaying because it hadnt loaded yet. Basicly to get the job done, I loaded the onLoad (retrieve the data) first, then and that would load my function to run the rest of my movie, now the var are available anywhere thanks again. I have added my code incase anyone else has this issue.