i’m confused with the term “global” in AS3. within my main controller class, i’ve defined a variable (myNum) which i would like to access throughout all class .as files within the same package.
//Controller class
package myApp
{
public var myNum:int = 24; //try to access myNum in mySprite class
public class Main extends Sprite
{
}
}
______________________________
//Object class
package myApp
{
public class mySprite extends Sprite
{
trace (myNum);
}
}
the above code returns the following error:
5006: An ActionScript file can not have more than one externally visible definition
what is the proper way to set up a list of global variables that can be accessed throughout the entire scope of a package?
Static variables could accomplish this.
You can declare a variable as
internal staticso that other classes in the same package can use it. Or you can declare it aspublic staticso that all classes in your project can use it, even if they’re in a different package.In the code below,
myNum1is accessible by classes in the same package, whereasmyNum2can be accessed from anywhere.Example of access from the same package:
Example of access from a different package:
Internal is actually the default access modifier in AS3, so writing
internal static varis the same as writing
static var. But it is probably better to writeinternal static varanyway just to be clear.PS It’s unclear from your example whether you’ve placed the two classes in one or two files. If you’ve placed them both in only one file, bear in mind that one AS file can only contain one class.