12 Replies Latest reply on May 22, 2007 7:21 AM by padrepio

    AS 3.0 Loading external text from a text file

    padrepio Level 1
      OK, so things have changed, a lot. In AS 2.0, I could create a text or HTML file with variables and have dynamic text fields to display the text from the loadVariablesNum argument. AS 3.0 uses a totally different model. How can I simply grab text from an external document and display it in a dynamic text field?
        • 1. Re: AS 3.0 Loading external text from a text file
          Level 7
          padrepio,

          > OK, so things have changed, a lot.

          Between AS2 and 3? Yes. :)

          > In AS 2.0, I could create a text or HTML file with variables
          > and have dynamic text fields to display the text from the
          > loadVariablesNum argument.

          loadVariablesNum() is a function, actually, and is similar to
          loadVariables(). There's also the LoadVars class, whose methods comprise
          similar functionality yet again.

          > AS 3.0 uses a totally different model. How can I simply grab
          > text from an external document and display it in a dynamic
          > text field?

          Here's a quick sample snippet.

          // Instantiate the URLLoader class
          var loader:URLLoader = new URLLoader();

          // Set the URLLoader's dataFormat property
          // to the static constant shown
          loader.dataFormat = URLLoaderDataFormat.VARIABLES;

          // Load the text file, making sure the path
          // itself is an instance of URLRequest
          loader.load(new URLRequest("data.txt"));

          That's it, really. Just three lines, so it's really not so bad. :)
          The data.txt reference show contains the following string:

          a=apples&b=bears&c=carrots

          To use the data as they become available, handle a complete event for
          that URLLoader instance ...

          loader.addEventListener(
          Event.COMPLETE,
          function(evt:Event):void {
          trace(evt.target.data.a);
          trace(evt.target.data.b);
          trace(evt.target.data.c);
          }
          )

          Note that the incoming parameter of the function literal (an arbitrary
          "evt") is an object that contains a target property. That target property
          refers back to the loader (URLLoader) instance, which in turn has a data
          property. The data property contains the three variables noted in the same
          text file.


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: AS 3.0 Loading external text from a text file
            Level 7
            Take a look at:
            http://www.kirupa.com/forum/showthread.php?p=1910480#post1910480

            "padrepio" <webforumsuser@macromedia.com> wrote in message
            news:f2k5o8$1pv$1@forums.macromedia.com...
            > OK, so things have changed, a lot. In AS 2.0, I could create a text or
            > HTML
            > file with variables and have dynamic text fields to display the text from
            > the
            > loadVariablesNum argument. AS 3.0 uses a totally different model. How can
            > I
            > simply grab text from an external document and display it in a dynamic
            > text
            > field?
            >


            • 3. Re: AS 3.0 Loading external text from a text file
              padrepio Level 1
              OK, that's making a little more sense and I did see that post on Kirupa. What's missing from both examples is how I get the text into a Dynamic text field? So instead of this:

              oader.addEventListener(
              Event.COMPLETE,
              function(evt:Event):void {
              trace(evt.target.data.a);
              trace(evt.target.data.b);
              trace(evt.target.data.c);
              }
              )

              Would I take out the trace statement and replace it with this?

              oader.addEventListener(
              Event.COMPLETE,
              function(evt:Event):void {
              home_txt.text = evt.target.data.a;
              products_txt.text = evt.target.data.b;
              services_txt.text = evt.target.data.c;
              }
              )

              • 4. Re: AS 3.0 Loading external text from a text file
                padrepio Level 1
                I tried that and got this error:

                TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@d59a4a9 to flash.display.MovieClip.

                Will Flash only import from a .txt file or will an .html file be OK too?

                Here are the variables from my file:

                home=Home&games=Games&activities=Activities&clubhouse=Club House

                Here is my AS code:

                // Instantiate the URLLoader class
                var loader:URLLoader = new URLLoader();

                // Set the URLLoader's dataFormat property
                // to the static constant shown
                loader.dataFormat = URLLoaderDataFormat.VARIABLES;

                // Load the text file, making sure the path itself is an instance of URLRequest
                loader.load(new URLRequest("buttons.html"));

                loader.addEventListener(Event.COMPLETE, function(evt:Event):void {
                home.htmlText = evt.target.data.home;
                games.htmlText = evt.target.data.games;
                activities.htmlText = evt.target.data.activities;
                clubhouse.htmlText = evt.target.data.clubhouse;
                }
                )

                That function constructor looks really weird.

                • 5. Re: AS 3.0 Loading external text from a text file
                  kglad Adobe Community Professional & MVP
                  that data property is the entire string from your textfile. use trace(evt.target.data) to see what you're loading.
                  • 6. Re: AS 3.0 Loading external text from a text file
                    Level 7
                    padrepio,

                    > I tried that and got this error:
                    >
                    > TypeError: Error #1034: Type Coercion failed: cannot
                    > convert flash.text::TextField@d59a4a9 to
                    > flash.display.MovieClip.

                    I'm not sure what that error means, to be honest.

                    > Will Flash only import from a .txt file or will an .html file
                    > be OK too?

                    An HTML file *is* a text file, just formatted a paticular way.
                    Traditionally, this sort of loading is done with files that end in the .txt
                    file extension.

                    > Here are the variables from my file:
                    >
                    > home=Home&games=Games&activities=Activities&clubhouse=Club House

                    I would urlencode that string, which means you should replace the space
                    in "Club House" with a %20; in otherwords ...

                    ... &clubhouse=Club%20House

                    > Here is my AS code:
                    >
                    > // Instantiate the URLLoader class
                    > var loader:URLLoader = new URLLoader();

                    So far, so good. That's the same.

                    > // Set the URLLoader's dataFormat property
                    > // to the static constant shown
                    > loader.dataFormat = URLLoaderDataFormat.VARIABLES;

                    Stil the same.

                    > // Load the text file, making sure the path itself is an instance of
                    > URLRequest
                    > loader.load(new URLRequest("buttons.html"));

                    I would load this as buttons.txt.

                    > loader.addEventListener(Event.COMPLETE, function(evt:Event):void {
                    > home.htmlText = evt.target.data.home;

                    Is home a dynamic text field instance? Since your data aren't
                    HTML-formatted, I would use the TextField.text property ...

                    home.text = evt.target.data.home;

                    > That function constructor looks really weird.

                    What function constructor?


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 7. Re: AS 3.0 Loading external text from a text file
                      funonmars Level 1
                      something else I notice that's different about the text files, is that in previous Flash ActionScript the text file would be like this:
                      &painting1=dumbo ate hay&painting2=cinderella was here&painting3=frosty paws&

                      with those &'s on either end essential, but now you need to drop them:
                      painting1=dumbo ate hay&painting2=cinderella was here&painting3=frosty paws

                      works. I didn't need to urlencode the text.
                      • 8. Re: AS 3.0 Loading external text from a text file
                        Level 7
                        funonmars,

                        > something else I notice that's different about the text files,
                        > is that in previous Flash ActionScript the text file would
                        > be like this:
                        > &painting1=dumbo ate hay&painting2=cinderella was here
                        > &painting3=frosty paws&

                        Right ...

                        > with those &'s on either end essential,

                        Huh? Nah. It has always been like this: name/value pairs are
                        separated by the ampersand character. There shouldn't be one at the very
                        beginning or at the very end, but it doesn't hurt to have them there.

                        > but now you need to drop them:
                        > painting1=dumbo ate hay&painting2=cinderella was here&
                        > painting3=frosty paws
                        >
                        > works. I didn't need to urlencode the text.

                        If that's so, then it means Flash is getting more strict, which I would
                        argue is a good thing. The strings mentioned should always have been
                        urlencoded. The text field contains the same sort of string that would
                        appear in a query string, the kind that begins with a question mark and
                        lists name/value pairs separated by ampersands.

                        If what you're saying is true -- and I don't doubt you; just haven't
                        tested -- then this reminds me of switching to XHTML from the more relaxed
                        HTML. Many of the headaches in HTML come from its relaxed "standards" --
                        which, because they're so relaxed, often aren't standard at all.


                        David Stiller
                        Adobe Community Expert
                        Dev blog, http://www.quip.net/blog/
                        "Luck is the residue of good design."


                        • 9. Re: AS 3.0 Loading external text from a text file
                          kglad Adobe Community Professional & MVP
                          actually, beginning and ending ampersands are no longer optional. they must be removed. the flash compiler is not happy unless each ampersand is between a value and a variable.

                          what is nice, though is if you assign the loaders dataFormat to be TEXT, you can load a text file without requiring the mytext=blah blah blah sort of thing that was required in as2. you can now load a standard text file.
                          • 10. Re: AS 3.0 Loading external text from a text file
                            padrepio Level 1
                            I've tried changing the html file to a .txt file. I also added an ampersand at the beginning of my variables:

                            I'm getting this error now: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: file:///Macintosh%20HD/Users/alemieux/Desktop/Flash%5FActionScript3/LoadText/buttons.txt
                            at test_fla::MainTimeline/test_fla::frame1()

                            I get this error with or without the %20 in the Club House variable. Here are the variables in the text file:

                            &home=Home&games=Games&activities=Activities&clubhouse=Club House

                            • 11. Re: AS 3.0 Loading external text from a text file
                              kglad Adobe Community Professional & MVP
                              there should be no ampersand before home.

                              but that compiler error is caused by an incorrect path to buttons.txt.
                              • 12. Re: AS 3.0 Loading external text from a text file
                                padrepio Level 1
                                Never mind. I got it!!! The ampersand at the beginning is a BIG NO NO! Don't do it! It returns an IO Error on the text file.

                                Thanks to all.