plshalpIsux
plshalpIsux

Reputation: 3

If statement on global variable doesn't execute function gotoAndStop();

I'm making a simple concept game, in which I've made buttons which are targets, when the user clicks said targets, it executes this code:

on (release){
    _global.targetCount++;
    Target1._visible=false;


    if(_global.targetCount==3){
        gotoAndStop(4);
    }

}

the global variable was declared on the frame like this:

_global.targetCount = 0;

and the buttons do disappear when I click on them like they should, but as soon as I click the final 3rd one and it disappears, it doesn't successfully check that the if(_global.targetCount==3) and proceed to the 4th frame.

I've tried declaring the variable differently like so:

var targetCount:Number = 0;

also tried doing it like this but on using the check code button it said my syntax was wrong:

var _global.targetCount:Number = 0;

and calling every instance as just targetCount, but that didn't fix it either,

I've searched and tinkered with the code, but I can't find clear examples on global variables, the little I've used here I found by reading this:

https://www.kirupa.com/developer/actionscript/tricks/global.htm

So I was wondering if anyone here could help me by letting me know the many mistakes I've done, and how to improve them.

All help is gladly appreciated!

Upvotes: 0

Views: 61

Answers (1)

weroro
weroro

Reputation: 1702

Every keyframe on stage is new closure. If you have variable on frame 2 and you want change/set or read value of this variable on frame 3, that variable does not exist and it is undefined. If you will try increment that undefined value you get NaN and gotoAndStop(NaN) doing nothing.

Insert trace(_global.targetCount); between _global.targetCount++; and Target1._visible=false; for debug.

Upvotes: 0

Related Questions