I'm basically a noob in ActionScript
, and I've run into a problem. I want to load variables from a text file into the _root
of my movie. Like, the text file contains &var=value&var2=value2&
, and I want them to go straight into _root
so that I'd be able to call them as _root.var
and _root.var2
.
First, I tried using the obvious solution, loadVariables. I've done it through the following function:
function getState (save){
//loadVariables("getstate.php?save="+save, "/");
loadVariables("state.txt, "/");
}
I've tried loading it to "_root
" instead of "/
", but still nothing. The request gets sent, but the contents of the text file get lost in hyperspace on their way to my code.
So I tried using LoadVars instead of loadVariables. This function disappointed me because there wasn't the option of dumping the vars right into _root
, but I decided to try it anyway. Here's how my function looks like with LoadVars:
function getState (save){
varReceiver = new LoadVars();
varReceiver.onLoad = function(){
varsRecieved=true;
_root.notloggedin = this.notloggedin;
_root.nostate = this.nostate;
_root.saveName=this.saveName;
//and lots of other variables
trace (_root.saveName);
};
varReceiver.load("state.txt");
trace (_root.saveName);
}
The trace that is inside the onLoad function returns the correct value, but the one after it returns undefined. I've tried waiting for the onLoad to happen with this loop:
while (varsRecieved != true){
trace ("not yet...");
}
Which sent Flash into an endless loop.
I'd really appreciate some help on this matter =)
Put the code that needs to be executed after the information has been loaded in a separate function and call that function at the end of the onLoad
handler:
function getState (save){
varReceiver = new LoadVars();
varReceiver.onLoad = function(){
_root.notloggedin = this.notloggedin;
_root.nostate = this.nostate;
_root.saveName=this.saveName;
//and lots of other variables
trace (_root.saveName);
doRest();
};
varReceiver.load("state.txt");
}
function doRest()
{
// ...
}
Note that getState()
will return before the information has been loaded, so existing code may have to be moved to doRest()
.