-
loading variable value from txt file
Here is my code. It doesn't work at all. In my txt file (points.txt) all it says is:
Germany = 5
code:
var loader:URLLoader = new URLLoader(new URLRequest("points.txt"));
loader.addEventListener(Event.COMPLETE, varsLoaded);
function varsLoaded(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
var variables:URLVariables = new URLVariables(loader.data);
var G:Number = variables.Germany;
trace(G);
myTextField.text = G;
}
Anyone know if there is a special way to set up the external txt file or if i'm doing something wrong with my code?
S
-
.
Hi,
You basically just needed to remove the spaces from your text file, if you wish to add more vars, then something like
text file
Code:
Germany=5
&England=3
and flash file
PHP Code:
import flash.text.TextField;
import flash.net.URLLoader;
import flash.net.URLRequest;
var loader:URLLoader = new URLLoader(new URLRequest("points.txt"));
loader.addEventListener(Event.COMPLETE, varsLoaded);
function varsLoaded(event:Event):void
{
event.target.removeEventListener(Event.COMPLETE, varsLoaded);
var variables:URLVariables = new URLVariables(loader.data);
var G:Number = variables.Germany;
var E:Number = variables.England;
trace(G);
trace(E);
myTextField.text = String(G);
myOtherTextField.text = String(E);
}
-
it actually didn't have a space in there so how is it that it's working now by adding in event.target.removeEven..... .. do you mind explaining this to me? Many thanks for your help
Also do you know if it's possible to load a text file like this from dropbox if there was a url to it?
-
.
Hi, Off the top of my head I do not know why it works witht the removeEvent added, The code is somewhat different from yours though, compare them.
As for dropbox, I have never tried to load a txt file from there. try it and see, you should need to know the var names of course, or load the text as a whole!!
-
Fruitbeard - i double checked it and the code is the exact same just that one line (removeEven) is different... it's confusing me because i would think if you stop the loop (removeListener) then it wouldn't execute especiallly since it's added to the top.
Also FYI box.com and probably dropbox can load txt file from there.
-
.
Hi,
Your code almost worked without my add ons, did you receive any errors?
Particularly this error: Scene 1, 1067: Implicit coercion of a value of type Number to an unrelated type String.
where as the textfield needed to be formed into a string, String(G).
It is generally a good idea to remove an event listener if it is not needed anymore(it can always be added again later if needed).
you also do not need the extra: var loader:URLLoader = URLLoader(event.target);
your code will work like so
var loader:URLLoader = new URLLoader(new URLRequest("points.txt"));
loader.addEventListener(Event.COMPLETE, varsLoaded);
function varsLoaded(event:Event):void {
var variables:URLVariables = new URLVariables(loader.data);
var G:Number = variables.Germany;
trace(G);
myTextField.text = String(G);
}
Good to see that you tested the external text file loading from the web sites, now you knwo it works.
-
Designer, Programmer, Musician
Hello there,
Actually, the texteditor adds formatting to the text, linebreaks and spaces. To fix that, just do this:
PHP Code:
var Str:String = e.target.data.Germany; Str = unescape(escape(Str).split("%0D%0A").join("")); trace(Str.length); trace(Str);
With that code, it doesn't matter how you write your variables in the texfield
var1=value1&var2=value2&var3=value3
or
var1=value1
&var2=value2
&var3=value3
It will work. Basically we are removing the unicode %0D%0A characters for spaces and linebreaks.
I've not tried the URLVariables to see if it gets rid of this, but in the meanwhile this script works for you.
Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|