English 中文(简体)
闪S3全球可变性?
原标题:Flash AS3 Global Variables?

a. 健康保险,即主要班级

//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??

• 如何使用AS3中的全球变量。

问题回答

如果你绝对必须具备全球3个变量,那么你就能够永远在你的最高级来源中形成一个文件,如:

MULTIPLIER.as

package
{
    public var MULTIPLIER:int = 3;
}

然后,在需要你的倍增效应时,你可以参考:

DoSomeMultiplying.as

package multiplying
{
    public class DoSomeMultiplying
    {
        public function multiplyMe(n:int):int
        {
            var m:int = n * MULTIPLIER;
            MULTIPLIER = m;
            return m;
        }
    }
}

然而,我建议你不要这样做!

但现在有可能在缺省一揽子方案中形成一个全球性的变数或恒定,作为全球不变或变数。

www.un.org/Depts/DGACM/index_spanish.htm A. 宣布全球功能

请注意,你也可以以同样的方式创造全球功能,而且你不需要使用进口说明(类似于内在追踪功能):

问候。

package {
  public function greet():String { return "Hello World" }
}

与全球变量类似,这一全球功能可在没有进口说明的任何地方获得:

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

全球测试不是一个全球变量,而是<代码>的一个公共实例变量。 主要类别。 不能使用<代码>Main类别物体,因为没有物体,就没有财产。 偶然变量与物体挂钩。

如果你想要获得的财产与任何特定物体无关,则将其宣布为<编码>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);
        }
    }
}

从你想要从以下各级提一下主要时间表:

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

这应当永远可行。

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.

我并不认为有实际的全球变数,但我看不出为什么需要。 如果你想要一个等级的变量能够从外部改变,只是宣布它静态,那么,它就将Name.variableName = 。

我同意其他人刚才所说的话,避免全球变数,更倾向于不变(通常不变并不意味着改变)

//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);
    }
}

从你想要从以下各级提一下主要时间表:

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

这应当永远可行。

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

23倍于:

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

您可使用“全球关键词......”。

前称“_global.as”的文档。

public var _global:* = activateGlobals();

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

之后,你可以测试——全球,如“Main2.as”类(有):

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();
    }
}
}

或许你可以提出<条码>。 Global:string = “testValue”; in Frame 1





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

Red5 Security Tutorial

I am looking for a step by step tutorial on securing Red5 from intrusion. This seems to be a question that comes up alot in a google search, but is never really answered in a way that makes sense to ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

visible property of DisplayObject

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set mc1.visible = false; mc2.visible stays true. Is that supposed to happen?...

热门标签