How to share an Array between all Classes in an application?

后端 未结 3 693
时光取名叫无心
时光取名叫无心 2021-01-21 22:10

I want to share an Array which all classes can \"get\" and \"change\" data inside that array. Something like a Global array or Multi Access array. How this is possible with Acti

3条回答
  •  面向向阳花
    2021-01-21 22:32

    NOTE: I discourage the use of Global Variables!

    But here is your answer

    You can go to your default package and create a file with the same name of your global variable and set the global variable public:

    //File: GlobalArray.as
    
    package {
        public var GlobalArray:Array = [];
    }
    

    And that's it! You have a global variable. You can acces from your code (from anywhere) like this:

    function DoSomething() {
        GlobalArray.push(new Object());
        GlobalArray.pop();
        for each (var object:* in GlobalArray) {
            //...
        }
    }
    

提交回复
热议问题