7 Replies Latest reply on Jan 3, 2007 12:55 PM by Rothrock

    Bug or ??

    Knud59
      I am building a hangman game (Flash 8), where users can supply their own list of words in an external text file. I am using the LoadVariables function and the method for loading variables from an external text file described in the help system, and this works fine - as expected - as long as the external text file does not contain any of the special Danish characters ("æ", "ø" and "å" - I don't know how these come through on a non-Danish system. Url-encoding the characters as suggested in the help for loadVariables, %e6, %f8 and %e5, gives the same error as typing them to the text file from the keyboard).
      The error is connected with the reading of the text file - I think.

      Here is the text file, named "words.txt" created in Notepad (on a Windows system):
      var1=ost,sko,l%E5sabcd&done="done"

      I have reproduced some of my program to demonstrate the error:

      var wordlist:Array = new Array();
      var letter, textOut:String;
      this.createEmptyMovieClip("target_mc", this.getNextHighestDepth());
      loadVariables("words.txt", target_mc);
      var param_interval:Number = setInterval(checkParamsLoaded, 100);

      // A movie clip showing a capital A
      a_key.onRelease = function() {
      letter = "A";
      updateOut();
      }

      // A movie clip showing a capital Æ (%c6)
      ae_key.onRelease = function() {
      letter = "Æ";
      updateOut();
      }
      function initGame() {
      s = "";
      wordlist = target_mc.var1.split(","); // split the list of words to single items
      for (i=0; i<target_mc.var1.length; i++) {
      s = s + target_mc.var1.charCodeAt(i) + "-";
      }
      fromFile_txt.text = s;
      trace(wordlist);
      textOut = "";
      }
      function updateOut() {
      textOut = textOut + letter;
      out_text.text = textOut;
      }

      function checkParamsLoaded() {
      if (target_mc.done == undefined) {
      trace("Not yet !");
      } else {
      trace("Done loading words");
      clearInterval(param_interval);
      initGame();
      }
      }

      Running this with Test Movie gives:
      in the Output panel:

      Done loading words
      ost,sko,l�bcd

      in the Flash movie:

      111-115-116-44-115-107-111-44-108-65533-98-99-100-

      corresponding to:

      o s t , s k o , l ??? b c d

      This means, that the special Danish character has been replaced along with the "s" and the "a" from the original input with a charactervalue of 65533

      I get the same result when running the exported swf file outside the Flash environment.

      I have tried - upon a previous response from kglad - thanks - to embed the font Latin 1 in the movie, but with no change in performance. I have also tried adding the font I am using (Tahoma) to the library. The only effect of these two efforts beeing to increase the size of the swf file

      Is this a bug - or am I missing something?
        • 1. Re: Bug or ??
          Rothrock Level 5
          Are you sure that the external text files is saved as UTF-8?

          In the testing environment, after the file is loaded, go Debug-> List Variables. Does it show correctly there?
          • 2. Re: Bug or ??
            Knud59 Level 1
            I'm not sure how to save as UTF-8 from Notepad. I have examined the file (words.txt) in a hex-editor, and every character is represented with the correct ASCII value (using the url-encoded version of words.txt).

            Using only English characters everything works fine. I can use <space> as well as the url-encoded +. Both work fine and produce the same output.

            Using Debug -> List Variables shows exactly the same content as the (erroneous) output from the program. The "character" with code 65533 is represented as a square in the output and using List Variables.
            • 3. Re: Bug or ??
              Rothrock Level 5
              Okay. Using the List Variables is sort of a good check point. If it shows right there then you know the problem is with how you are displaying the info and the embedding of Latin 1 (or additional characters) will be most likely be the solution.

              That it isn't showing properly there means the problem is with the file or the reading part. The best and easiest solution would be to figure out how to save the file as UTF-8. Flash really likes that. I'm on a Mac so I can't tell you how to do it from Notepad, but on TextEdit (the Mac analog) I would do a save as and there would be a drop down to allow me to select different formats. I imagine there is something similar in Notepad.

              If you can't do it, or you are interfacing with some other web technology that doesn't speak unicode, I'm not sure I know how to help you. Have you tried using unescape()? I played around with the letters – loading them in as unicode and then doing escape(). I found that Flash would encode them as:

              escape("æ")=%C3%A6
              escape("ø")=%C3%B8
              escape("å")=%C3%A5

              So I'm thinking that Flash uses a different urlencoding scheme that some other systems. Really unicode is a good thing.

              Also just as a note, you might want to use LoadVars class instead of the old-school loadVariables. LoadVars has a very nice onLoad event that you can use instead of having to keep checking if the file has loaded. And depending you might even want to change the word list into an XML document.
              • 4. Re: Bug or ??
                Knud59 Level 1
                Sometimes you just need reminding!
                Having used Notepad a lot I have never cared about the "Save as" option - but there it was "Save as UTF-8". I did and it worked, that is if I used the keyboard version, not if using the url-encoding.
                Thanks a lot.
                Your suggestion about an XML-input is worth considering, allthough I need for other people to be able to write their own input file. Almost everybody will be familiar with Notepad, and can understand instructions about "Save as UTF-8". Trusting them to do a correct XML file, well I think that is a little too complex for the people I have in mind.

                Thanks again for your time and interest
                • 5. Re: Bug or ??
                  Rothrock Level 5
                  Glad that worked. If you are going to have other people make their own files I would definitely look into using LoadVars. I know a lot of truly lovely people, but if a few XML tags are above their heads, then &done="done" will probably do a few of them in as well! I promise that it will happen as:

                  &DONE="DONE"
                  &Done="done"
                  &done=done

                  and so on.
                  • 6. Re: Bug or ??
                    Knud59 Level 1
                    Hi again
                    Thanks for the help and for pointing to a problem in my solution. I have done some thinking and reading and ended with this:
                    I am reading the external text file (in UTF-8 format) as an XML file.
                    Tricking Flash to do so gets me a single node as a string containing the entire text file. This string can then be parsed. The external text file then can be much simpler - a single word or phrase on its own line.
                    The reading and control of loading is then done with the XML object's load and onLoad methods.

                    I owe this solution to Edwin Heijmen from the book: Flash Hacks by Sham Bangal (O'Reilly publishing Co.), where it is hack no. 85:


                    (with a couple of my changes - on a Windows system a new line i s marked by two characters (\r \n) so you have to step two forward for the next starting index - not one as in the recipe. I also use the Array.push() in favor of explicitly assigning the value to a specific index.

                    • 7. Re: Bug or ??
                      Rothrock Level 5
                      Nice solution. Just want to point out that hacks such as that are a bit sloppy. Probably best when you alone will be creating the files. But if others do, can you be sure they won't create the text file on a Linux machine? Just be careful…