Globals in flash as3

Actionscript 3 doesn’t have build-in global variables. Globals are however easy to implement with just one small class with a public static object. Just three easy steps.

1) Make a new class with a public static variable (..or just download example files here)

package org.randomaccess.utility{
    public class Gb
    {
          public static var vars:Object = new Object();
    }
}

2) Import globals class to your own classes

import org.randomaccess.utility.Gb;

3) Use global variables like this:

Gb.vars.message_1 = "This is a global message";
Gb.vars.another_variable = "This is another global variable";

DOWNLOAD EXAMPLE FILES

Leave a Reply