all groups > flash actionscript > may 2007 >
You're in the

flash actionscript

group:

variable problems local and global score


variable problems local and global score vladimirj
5/19/2007 10:21:48 PM
flash actionscript:
Hi i'm having problem of writing a value to a global variable in a function and
using this variable affterwards. My problems is the following i have a
firstProduct variable that is initialized as global at the begining of my code
and the i have an XML on load function where i should take the first child and
write it to his variable and use it afterwards for passing it as a variable to
PHP but this is irrelevant, however when im trying to trace im getting
undefined any suggestions ?? here is the code



var theXML2:XML = new XML();
theXML2.ignoreWhite = true;
_global.firstProduct1="";

theXML2.load("http://localhost/functions/get_products.php?subcategory_name="+va
2);
this.theXML2.onLoad = function() {

var nodes2 = this.firstChild.childNodes;
//problem?!?!?
firstProduct1=nodes2[0].firstChild.nodeValue;

trace(firstProduct1);

for (i=0; i<nodes2.length; i++) {

productsList.addItem({label:nodes2[i].firstChild.nodeValue});
}


}
Re: variable problems local and global score kglad
5/19/2007 11:43:28 PM
every time you assign (or reassign) a value to firstProduct you should use the
_global prefix.

if your trace() is undefined immediately after assigning it to be that
nodeValue, your nodeValue is undefined.
Re: variable problems local and global score vladimirj
5/19/2007 11:57:36 PM
when i trace the variable inside the function on load it writes it but when i
try to access it outside it is undefined im really frustrated, how the heck i
assing values to variables in actionscript where i can access them from any
frame that i want. do u have any suggesstions?
Re: variable problems local and global score kglad
5/20/2007 5:09:28 AM
again, you must use the _global prefix each time you assign or reassign a value
to firstproduct:



var theXML2:XML = new XML();
theXML2.ignoreWhite = true;
_global.firstProduct1="";

theXML2.load("http://localhost/functions/get_products.php?subcategory_name="+va
2);
this.theXML2.onLoad = function() {

var nodes2 = this.firstChild.childNodes;
//problem?!?!?
_global.firstProduct1=nodes2[0].firstChild.nodeValue;

trace(firstProduct1);

for (i=0; i<nodes2.length; i++) {

productsList.addItem({label:nodes2[i].firstChild.nodeValue});
}


}
Re: variable problems local and global score vladimirj
5/20/2007 2:45:54 PM
i've tried that but no success i think the problem is the nested function as i
read on the net. i've made a new topic post of how to get an XML without using
onLoad (if it is possible) so no nested functions will be used. Thanks for your
help
Re: variable problems local and global score kglad
5/20/2007 3:22:06 PM
AddThis Social Bookmark Button