If you absolutely positively have to have a global variable in as3, you could always create a file in the top-level of your source folder like this:
MULTIPLIER.as
package
{
public var MULTIPLIER:int = 3;
}
Then, whenever you need your multiplier you could reference wherever you need it like this:
DoSomeMultiplying.as
package multiplying
{
public class DoSomeMultiplying
{
public function multiplyMe(n:int):int
{
var m:int = n * MULTIPLIER;
MULTIPLIER = m;
return m;
}
}
}
However, I would strongly recommend that you do not do this! it is horribly bad practice, horribly slow and, well, just horrible!
But there it is, it is possible to create a global variable or constant in the default package to act as a global constant or variable.
Declaring Global Functions in AS3
Note that you can also create global functions in the same way, and you don't need to use an import statement for (similar to the built-in trace function):
greet.as
package {
public function greet():String { return "Hello World" }
}
Similar to the global variable, this global function is accessible from anywhere without an import statement:
package bar {
public class foo
{
public function foo():void
{
trace("New foo says: "+greet()+", no import necessary");
// New foo says: Hello World, no import necessary
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…