7 Replies Latest reply on Oct 10, 2007 3:01 AM by Greg Dove

    Loading variables from txt

      Hello everybody,

      I have a flash file with two dynamic fields which get data from a txt file. There is one more dynamic field in flash which i want to display a certain calculation result between the values of the two other dynamic fields. The two vars are loaded from the txt file without any problem, but flash recognizes them as strings and as a result, the calculation is impossible (NaN). How can i convert the loaded data from the txt file into numeric ones in order to be able to do calculations between them?

      Thank you
        • 1. Re: Loading variables from txt
          Sivakanesh Level 1
          Have you tried converting them into Numbers or declaring the variable as Number? Try this;

          var a:String = "190"; //say this is loaded from the file
          var b:String = "100"; //so is this
          var c:Number = int(a);
          var d:Number = int(b);
          trace(c*d+c+d); //will give you 19290
          1 person found this helpful
          • 2. Re: Loading variables from txt
            Greg Dove Level 4
            You have a choice:

            if your string values is called myVar1

            You can convert it to a number type in the following ways:

            myVar1=parseInt(myVar1); //for parsing integer values, ignores after any decimal points - useful particularly for hex strings etc
            myVar1=parseFloat(myVar1); //parses the value into a regular precision number
            myVar1=Number(myVar1);


            You can read more about each of the above in the relevant links on the following page:
            http://livedocs.adobe.com/flash/9.0/main/00001166.html
            • 3. Re: Loading variables from txt
              Greg Dove Level 4
              Sivakanesh: That does seem to work for integer strings... but I don't think its intended/official. Or the documentation is wrong.
              It shouldn't work based on the documentation. int as a global function was deprecated in as2 but is back in fashion in as3, presumably because its a datatype. It *should* only accept a number argument. Or the documentation should be corrected I guess ( uint() as a function also seems to accept a string arg in as3 )
              1 person found this helpful
              • 4. Loading variables from txt
                How could i display the result in a dynamic field by doing the following?

                var a:String = "190"; //say this is loaded from the file
                var b:String = "100"; //so is this
                var c:Number = int(a);
                var d:Number = int(b);
                trace(c*d+c+d); //will give you 19290

                The output does give 19290, but i need this appearing in the swf!
                GWD' s solution doesn't work either! Could anybody help by providing both sample txt & fla files?

                Thanx
                • 5. Re: Loading variables from txt
                  Sivakanesh Level 1
                  Thanks GWD, good to know. But for the purpose of answering the last question;

                  original_21:
                  Do you have a TextBox on your stage? If not add one. In the properties of the textbox make it a Dynamic Text. Give it an Instance Name, say total_txt.
                  Then in your actionscript add the following line;
                  total_txt.text = c*d+c+d;

                  Remember to use fully qualified path to the text box.
                  • 6. Loading variables from txt
                    It finally worked (it just needed time - some more frames to read the txt) :

                    sample.fla :
                    frames 1-9 --> loadVariablesNum("data.txt", 0);
                    frame 10 -->

                    stop();

                    field0 = parseFloat(field0);
                    field0 = Number(field0);

                    field1 = parseFloat(field1);
                    field1 = Number(field1);

                    field2 = Number(field0-field1);

                    data.txt -->
                    &field0=4.18
                    &field1=4.15

                    So my only concern now, is how to keep only two decimal digitls of the output number!


                    Thank you guys!
                    • 7. Re: Loading variables from txt
                      Greg Dove Level 4
                      The way you're loading the text file and just waiting for 10 frames for it to load is unreliable. It may not work for example for people with slower connections. You need a more reliable way to wait for the data to be loaded. LoadVars is the easiest way to do this. I'll assume that your textfields have variable associations field0, field1, field2...

                      Try this. All on frame 1.
                      1 person found this helpful