Skip to content →

Creating Global Variables in AS3

This is one of the problems I’ve struggled with since switching from AS2 to AS3 programming. I know that globals aren’t encouraged and are considered bad practice by most professional coders, but should I need them (and I’m fairly confident I will,) I decided it would be a good idea to find out how to create and access them from any point in my FLA file.My first point of call was the forums. These are a goldmine for information on all things Actionscript, Flash and beyond. A post in the beginners section led to no replies, but I hadn’t given up hope of solving the problem.

A further dig through the site led to some interesting threads. The problem of creating global variables in AS3 seems to be common for new programmers and especially those moving from AS2 to AS3. Despite this, however, each problem posted had it’s own factors that muddied the solution. I moved on to Stack Overflow, which provided the answer (though it did take Google to find it.) The answer it seems is simple: declare the variable in my document class then reference it in other classes using main.varname. Simple!

For example: (document class)

package {
	import flash.display.MovieClip;

	public class main extends MovieClip {

		// public variables used throughout the game.
		// ******************************************

		public static var healthSpending:Number = 1;
		public var myTitleScreen = new titleScreen();

		public function main() {
			// Add relevant _mc to stage

package {
import flash.display.MovieClip;
public class titleScreen extends MovieClip {
public function titleScreen() {

An interesting discussion on the subject, as well as a great deal more information is available on Greenthumb. I’m particularly glad for this post, as it and the subsequent discussion in the comments generally cover everything I needed to know.


Published in Blog Flash

One Comment

  1. jessy jessy

    how to access the global variable from inside the fla file?

Leave a Reply

Your email address will not be published. Required fields are marked *