9 Replies Latest reply on Jun 15, 2006 9:28 AM by blemmo

    XML > Flash - URL string in loadmovie function

    Dennis_S.


      I'm using Flash 8 to read and parse an XML file into variables. One of the pieces of information I'm trying to be able to extract from the XML file is the path to an image (e.g. images/image1.jpg) to use in the loadmovie(url, method) fuction as the url:string. Everything is working great, except I'm trying to plug in the variable name for the url and it is coming back as undefined when I test the piece. I've tried just about everything I can think of, and can't find anything in the help system about adding quotes in the variable string in a manner that works with loadmovie.

      Any suggestions would be appreciated!

      Dennis S.
        • 1. Re: XML > Flash - URL string in loadmovie function
          blemmo Level 1
          I'm not sure if that's what you want, but you can simply add a variable to a string with the + operator:
          var theVar = "foo";
          var theString = theVar + " bar";
          trace(theString) // outputs "foo bar"

          If it's not what you wanted, please post some more info (code).

          cheers,
          blemmo
          • 2. Re: XML > Flash - URL string in loadmovie function
            Dennis_S. Level 1
            Thanks for the try.... Below is the code I used with comments. I tried concatenating the " on the beginning and end of the string, but could not get it to work properly.

            // Create new XML Object and set ignoreWhite true
            book_xml = new XML();
            book_xml.ignoreWhite = true;
            // Setup load handler which just invokes another function
            // which will do the parsing of our XML
            book_xml.onLoad = function(sucess) {
            if (sucess) {
            processBook(book_xml);
            }
            };
            // Load up the XML file into Flash
            book_xml.load('book.xml');
            // This is the function that will be called when
            // our XML document is loaded succesfully
            function processBook(xmlDoc_xml) {
            // xmlDoc_xml is now a reference to the XML
            // object where our information is stored
            // the content of the first three variables below display fine because the variables are
            // linked to a dynamic text field. The 4th is not working since I am using it to define
            // the URL in the loadmovie function below.
            topictxt1_var = (xmlDoc_xml.firstChild.firstChild.firstChild.firstChild.nodeValue);
            bodytxt1_var = (xmlDoc_xml.firstChild.firstChild.firstChild.nextSibling.firstChild.nodeValue);
            instructions_var = (xmlDoc_xml.firstChild.firstChild.firstChild.nextSibling.nextSibling.firstChild.nodeValue );
            media_var = (xmlDoc_xml.firstChild.firstChild.firstChild.nextSibling.nextSibling.nextSibling.firstChi ld.nodeValue);
            }
            //The problem here is that the variable media_var contains the string "images/award.jpg" without the quotes.
            //In the loadmovie function needs the quotes " at the beginning and end of the string images/award.jpg to define the URL.
            loadmovie(media_var, image_mc);

            Thanks again!
            • 3. Re: XML > Flash - URL string in loadmovie function
              blemmo Level 1
              XMLNode.nodeValue always returns a string, so you can just use the returned value for the loadMovie method. The problem must be something else. You mentioned it comes out as "undefined", so I'd guess you're not getting the right XML node. Check the path to the node, there must be something wrong with that. If you're getting to the right node, the media_var should contain a string and can be used for loadMovie().

              hth,
              blemmo
              • 4. Re: XML > Flash - URL string in loadmovie function
                Dennis_S. Level 1
                I have the correct node, because when I check the contents of the variables they all show the correct text from the xml file. The problem is that for the variable I want to use to define the URL in loadmovie(URL, target) format is coming in as:

                images/award.jpg

                and Not

                "images/award.jpg"

                It is the quotation marks that I can't seem to get into the variable (i.e. "images/award.jpg")

                Thank you for your help in solving this!
                • 5. Re: XML > Flash - URL string in loadmovie function
                  blemmo Level 1
                  Hm, if media_var gets defined, it should be a string. Try this:
                  trace("typeof media_var: "+typeof(media_var));
                  to see what type it is. It should be a string.

                  Is loadmovie a custom function? If not, it is spelled wrong, it should be loadMovie (capital M). Maybe that's the reason?

                  • 6. Re: XML > Flash - URL string in loadmovie function
                    Dennis_S. Level 1
                    I tried that, and it in fact is a string. I also renamed my function correctly (i.e. loadMovie), but I'm still getting the 'undefined' issue for the URL. I've tried to comment a little more in the text below. The problem seems to be in the last 4 or 5 lines where I'm trying to use a variable string to define the url instead of a hard coded string with " quotes ".

                    Thanks again! :)

                    // Create new XML Object and set ignoreWhite true
                    book_xml = new XML();
                    book_xml.ignoreWhite = true;
                    // Setup load handler which just invokes another function
                    // which will do the parsing of our XML
                    book_xml.onLoad = function(sucess) {
                    if (sucess) {
                    processBook(book_xml);
                    }
                    };
                    // Load up the XML file into Flash
                    book_xml.load('book.xml');
                    // This is the function that will be called when
                    // our XML document is loaded succesfully
                    function processBook(xmlDoc_xml) {
                    // xmlDoc_xml is now a reference to the XML
                    // object where our information is stored
                    // the content of the first three variables below display fine because the variables are
                    // linked to a dynamic text field. The 4th is not working since I am using it to define
                    // the URL in the loadmovie function below.
                    topictxt1_var = (xmlDoc_xml.firstChild.firstChild.firstChild.firstChild.nodeValue);
                    bodytxt1_var = (xmlDoc_xml.firstChild.firstChild.firstChild.nextSibling.firstChild.nodeValue);
                    instructions_var = (xmlDoc_xml.firstChild.firstChild.firstChild.nextSibling.nextSibling.firstChild.nodeValue );
                    media_var = (xmlDoc_xml.firstChild.firstChild.firstChild.nextSibling.nextSibling.nextSibling.firstChi ld.nodeValue);
                    }
                    //The problem here is that the variable media_var contains the string "images/award.jpg" without the quotes.
                    //In the loadmovie function needs the quotes " at the beginning and end of the string images/award.jpg to define the URL.
                    //This works>> loadMovie("images/award.jpg", image_mc);
                    //This doesn't
                    loadMovie(media_var, image_mc);
                    • 7. Re: XML > Flash - URL string in loadmovie function
                      blemmo Level 1
                      Try declaring the vars outside the parsing function:

                      book_xml = new XML();
                      book_xml.ignoreWhite = true;
                      media_var:String;
                      // other vars
                      book_xml.onLoad = ...

                      I think in your code, the vars are only defined local to the processBook function, so you can't access them outside that function. If you declare them outside the function, they're not local anymore, and should keep the value after processBook finishs.
                      • 8. Re: XML > Flash - URL string in loadmovie function
                        Dennis_S. Level 1


                        Well, that didn't seem to work either. I'm at a loss for how to do this now. Thanks for the help!
                        • 9. Re: XML > Flash - URL string in loadmovie function
                          blemmo Level 1
                          aargh... finally I know what's wrong: you're calling loadMovie right after the XML.load call. Because XML.load is an asynchronous method, the code execution doesn't wait for a result and goes on. So the loadMovie call comes when the loading is still in progress, and media_var is still undefined.
                          Just place the loadMovie in the processXML function, after the xml parsing. Then it gets executed as soon as the xml is loaded and parsed.

                          cheers,
                          blemmo