Groups | Blog | Home
all groups > flash actionscript > march 2004 >

flash actionscript : Global Variable Discrepancy


pharaohman98
3/28/2004 9:13:45 PM
Hi all.
Issue number 2 on my list...thankfully it should be easier to follow than my
previous question.
In my timeline I have a layer called ACTIONS that houses a global variable. I
have two other layers (PLAYER1 and PLAYER2) which each contain an mc that needs
to access the global variable in the ACTIONS layer. The mc in PLAYER1 reads in
the global variable's boolean value (which is defaulted to FALSE) and then
makes it TRUE. However, when I trace the value of the global variable from
both the PLAYER1 and PLAYER2 mcs, only PLAYER1 says it has changed to
TRUE...PLAYER2 still outputs it as FALSE. Any ideas why it's not "seeing" the
change? As always, your help is greatly appreciated!
stwingy
3/28/2004 9:33:08 PM
try this
_global.myvar = false;
mc1.onEnterFrame = function() {
if (myvar == false) {
myvar = true;
delete onEnterFrame;
trace(myvar);
checker();
}
};
function checker() {
mc2.onEnterFrame = function() {
trace("mc2 myvar="+myvar);
delete this.onEnterFrame;
};
}
pharaohman98
3/29/2004 5:41:45 AM
Alright, I put in the test code and both of the outputs say true, but I'm
confused as to where the two functions go. When I output my own trace from the
PLAYER2 mc it still says false. Currently I put all the test code into the
PLAYER1 mc, which did the alteration on the global variable. Am I putting it
in the wrong spot? Sorry, I guess my debuggins skills aren't too up-to-speed.
stwingy
3/29/2004 5:13:51 PM
I tend to keep all code together in one frame if possible. If you want to
change a _global variable from a different timeline then you need a fully
qualified reference to it.
In your example you could try this (fingers crossed)
main timeline
_global.myVar = false;
mc1 timeline
trace(myVar);
if (myVar == false) {
_global.myVar = true;
}
trace(myVar);

mc2 timeline
trace("mc2 "+myVar)
pharaohman98
3/29/2004 8:39:45 PM
Perfect! Okay, so after combining codes I just realized that my assignment
statement originally read [i]myvar=true;[/i]
And to fix it all I did was append [i]_global.myvar=true;[/i]
So is this sort of a rule-of-thumb with global variables? In order to
reference their value you only need to type [i]myvar[/i], but to ALTER their
value you need to have a "qualified reference," as you put it, like
[i]_global.myvar[/i]?
In any case, thanks again for the help!
AddThis Social Bookmark Button