7 Replies Latest reply on Sep 4, 2006 9:53 PM by ali_kiyani

    How to read from text file?


      I am trying to create a simple SWF file in Flash MX which will read contents from a text file named "file.txt" and display on screen. Only one word is written in that text file "hello" which needs to be displayed.
      I tried using loadVariables function but somehow it is not working and nothing prints on the screen.

      This is my first flash animation in more than 6 years (I used to do flash 4 in 1998) so I haven't done action scripting before.

      Please take a look at the file I created by clicking the URL given below and tell me why it isn't printing anything on the screen after reading from text file.

        • 1. Re: How to read from text file?
          Tal_CS Level 1
          Hi Ali,
          you need to write pairs of variableName= variableValue in your text file; not just "hello", as you stated.
          AN example from the manual:


          Also, you wrote "holder.loadVaribles[...]", which is wrong: you just need to write at the txt file "holder=hello"
          and the action loadVariables("textFile.txt"), which will load the variable holder into the _root of the movie.
          the dynamic text will then update properly since you identified it with the variable holder.
          • 2. Re: How to read from text file?
            ali_kiyani Level 1
            Ok I made the following two changes like you said:

            1. In the text file I wrote "holder=hello"
            2. In fla file I change the line holder.loadVariables("file.txt") to loadVariables("file.txt")

            But still it doesn't print anything. I think I am missing something because you said it will load the variable in _root of the movie. How do I access _root?
            • 3. Re: How to read from text file?
              Tal_CS Level 1
              I see.
              I looked again in the Manual under loadVariables. It seems you need to load the variables into a movie clip.
              Try creating an empty MC on the stage, give it an instance name and load the varibles into it:

              loadVariables("file.txt", variables_mc);

              then, you'll have variables_mc.holder (= hello)
              and you could assign it as holder.txt = variables_mc.holder;

              I remember there was a simpler way, because now you'll need to monitor when the varibles are loaded etc.
              See the manual for example.
              • 4. Re: How to read from text file?
                Hey ali,

                The following code got me out of a bind. It is mostly from the Flash help files, however, I also included some code that gets rid of any extra cariage returns and line breaks.

                • 5. Re: How to read from text file?
                  ali_kiyani Level 1
                  Many thanks Tal_CS it worked! :)
                  iceman haven't tried your code yet, because I don't know where to put it hehehe, but I assume it needs to be written in the same way as loadVariables(...), thanks!... will try it later
                  • 6. Re: How to read from text file?
                    Blakice_iceman Level 1
                    No problem ali. The code I showed you would go in the actions layer on the same frame as the dynamic text field that holds the "file.txt".