mercoledì, marzo 05, 2008

Global Variables in AS3

I’ve read it’s better to try and avoid global variables…but I really enjoyed them in AS2. Thanks to this post on Ultrashock, there is a workaround to accomplish the same sort of behavior even though _global doesn't exist in AS3.

“If you want something that acts like _global then you would need to create a class which contains static properties.”

package{
public class MyGlobal {
public static var something:String;
}
}

“Then you import that class when you need to use it and access the properties like this.”

import MyGlobal;
MyGlobal.something = "whatever";

You can now store any variables/object in the Global class without having to define each one:

Global.data.name = "Player One";
Global.data.score = 1000;