actionscript-3flashvariablesflash-cs5flash-cs5.5

How to define global variables in flash cs5.5?


I can't find if there is a way to define a variable which can be accessed from anywhere in the project in Actionscript 3.0 using Flash CS5.5.

I tried this:

_global.myScore = 0;

which i got from adobe_website but it didn't work.

Can anyone help or give any clue?

EDIT: I just tried the most easiest way and it looks like it's working but i'm not sure. I just created a new layer and i defined the variable in the first frame:

var myvar:int=0;

Is this way correct?


Solution

  • Try static variable, declared in some class:

    public static myScore:int = 0;
    

    use:

    SomeClass.myscore += 1;
    

    but be careful not to use them too much or too often - in terms of optimization, they are not so good.

    Static members are members of the class, not the instance, and you can't use them within regular function - the function has to be static too.