HI i have a main class
//main.as
package {
public class main {
public var testGlobal:string = "testValue";
}
}
//pop.as
package {
public class pop {
function pop():void {
trace("testGloabl from main.as" + testGlobal);
}
}
}
How can i get the testGlobal value on pop.as width out using a main class Object. Is there any method of Global variables??
How to use global variables in AS3 .
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
}
}
}