开发者

Flash AS3 Global Variables?

开发者 https://www.devze.com 2022-12-13 18:55 出处:网络
HI i have a main class //main.as package { public class main { public var testGlobal:string = \"testValue\";

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
        }
    }
}


testGlobal is not a global variable, it is a public instance variable of the Main class. You cannot access it without using a Main class object, because without an object there is no existence for a property. Instance variables are tied to objects.

If you want to access create a property that is not tied to any particular object instance, declare it as static.

//Main.as
package {
    public class Main {
        public static var testGlobal:string = "testValue";
    }
}


//Pop.as
package {
    public class Pop {
        function pop():void {
                trace("testGloabl from Main.as" + Main.testGlobal);
        }
    }
}


Just do this from any level you want to call the main timeline from:

//Define your global variable
var myGlobalVariable:String = "Get Up!";
//call it from anywhere
var Root:MovieClip = root as MovieClip;
trace(Root.myGlobalVar);

That should work anytime.


You can use the static, like it was said before, or you can use the Singleton pattern. There are no private constructors in AS, so what you can do is: a) be very carefull not to call the constructor b) send an exception everytime someone calls the constructor and the instance has already been set.

I don't think there are actual global variables - but I don't see why you would need any. If you want a variable in class to be modifiable from the outside just declare it static then do className.variableName = .


I agree with what the others just said, avoid global variables, prefer constant (and usually constants are not meant to be changed)


//main.as

package {
    public class main {
        public var testGlobal:string = "testValue";

    }
}


//pop.as

package {

public class pop {
    function pop():void {
        trace("testGlobal from main.as -- " + new main().testGlobal);
        // or
        var m : main = new main();
        trace(m.testGlobal);
    }
}


Just do this from any level you want to call the main timeline from:

//Define your global variable
var myGlobalVariable:String = "Get Up!";
//call it from anywhere var Root:MovieClip = root as MovieClip;
trace(Root.myGlobalVar);

That should work anytime.

var i:int = 10000000;
while(i--){
    Root.nn--;
}

23 times slower than:

var i:int = 10000000;
while(i--){
    globalVar.nn--;  // package
}


You may use the _global keyword...

ex : a file named "_global.as" with this code:

public var _global:* = activateGlobals();

public function activateGlobals():*{
    if(_global == null){
        _global = this;
    }
    return _global;
}

Then you can test _global as hereunder in a class eg "Main2.as" (have fun):

package classes {
import flash.display.Sprite;
public class Main2 extends flash.display.Sprite{
    include "_global.as";
    var globalMethod:Function;
    var globalID;
    var globalValue:Number;
    var innerMethod:Function;
    var factorial:Function;
    var prototype = Main2.prototype;
    Main2.prototype.globalID = "globalIDprototype";
    Main2.prototype.globalValue = Math.PI*2;
    public function Main2() {
        _global.factorial = function(n:Number) { 
            if (n<=1) { 
                return 1; 
            } else { 
                return n*_global.factorial(n-1); 
            } 
        } 
        prototype.setPropertyIsEnumerable("globalID",true);
        _global.globalMethod = function(){
            trace("Use of the _global method \"globalMethod()\"");
        }
        _global.globalID = "_global id";
        _global.globalValue = Math.PI;
        _global.globalMethod();
        // access properties :
        trace(_global.globalID);
        trace(_global.globalValue);
        trace(prototype.globalID);
        trace(prototype.globalValue);
        listValues();
        getNameInInnerMethod();
        _global.globalMethod();
        getNameInInnerMethod();
    }

    private function listValues():void{
        for(var i in prototype){
            trace(" - prototype[" + i + "] = " + prototype[i]);
        }
        for(i in _global){
            trace(" - _global[" + i + "] = " + _global[i]);
        }
        i = null; // optionnal
    }
    private function getNameInInnerMethod():void{
        _global.globalMethod = function(){
            trace("this.name = " + this.name);
            trace("_global.name = " + _global.name);
        }
        function innerMethod():void{
            trace("this.name = " + this.name);
            trace("_global.name = " + _global.name);
            // with global you have access to the name property from the inner method.
        }
        innerMethod();
    }
}
}


Maybe you can place var testGlobal:string = "testValue"; in Frame 1

0

精彩评论

暂无评论...
验证码 换一张
取 消