actionscript: global variables
- Started
- Last post
- 6 Responses
- cosmo
I am trying to read a php file using as. Which is working just fine, but once I try to access the variable filename outside the function it doesn't seem to work. Any suggestions?
var fileName = "";
var receiveData = new LoadVars();
receiveData.sendAndLoad("http... receiveData, "POST");
receiveData.onLoad = function(success){
if(success){
fileName = receiveData.urlValue;
trace (fileName);
}
}
- tomkat0
try 2 things:
- put the sendAndLoad command AFTER the definition of the function, now you load before the function is declared
- check the var type, now you have string assigned (by using file="") better use:
var myVar:String;
or if you are unsure
var myVar:Object;
hope it helps
- nUBBER0
scoping issue perhaps? the onLoad function is in the scope of the loadvars instance.
var scope = this;
var fileName = "";var receiveData = new LoadVars();
receiveData.onLoad = function(success)
{
if(success)
{
scope.fileName = this.urlValue ;
trace (scope.fileName);
}
}receiveData.sendAndL oad("www.blah blah.com/list.php", receiveData, "POST");
- undo_undo0
_global.yourVarName makes the variable accessible anywhere in the swf
- nUBBER0
better to create a var in functions scope than create _global vars...
- st33d0
I've made a whole site based on this scoping issue.
The issue is that LoadVars cannot see outside itself - so you have to put a reference to the outside INSIDE LoadVars.
This becomes tricky when doing it in an AS2.0 class, you have to use ["thisCrap"] to trick the compiler into letting you define a variable that doesn't exist.
var fileName = "http://newstoday.com";
var myLoadVars = new LoadVars();
myLoadVars["MC"] = this;
myLoadVars.onLoad = function(success){
if(success){
trace(this["MC"].fileName);
}
}
myLoadVars.load(fileName);
- cosmo0
_global.yourVarName worked. Thanks guys.