您好我有一个主类
//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);
}
}
}
我怎样才能在pop.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??
如何在AS3中使用全局变量。
How to use global variables in AS3 .
如果你绝对肯定必须有在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
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
DoSomeMultiplying.as
package multiplying
{
public class DoSomeMultiplying
{
public function multiplyMe(n:int):int
{
var m:int = n * MULTIPLIER;
MULTIPLIER = m;
return m;
}
}
}
不过,我的的强烈建议你不要这样做!的这是可怕的不好的做法,可怕的缓慢,那么,就可怕!
但是它是,它可以创建在默认包一个全局变量或常数充当全局常量或变量
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.
声明全局函数在AS3
请注意,您也可以以同样的方式创建全局的功能,而你并不需要使用的import语句(类似内置跟踪功能):
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
}
}
}