开发者

Simulate AS2 _global variable using a simple class in AS3

I have recently started making (and am still making) the transition from ActionScript 2 to 3. I have used the _global variable in an AS2 project as I needed a variable to be accessible from within ANY swf loaded into my main movie. This worked like a treat.

However, along came AS3 and whipped that from right under my feet. After much Googling, a few people suggested making use of a simple class with a simple variable that can be publicly set and retrieved. I made a simple class, imported it and manipulated the value, and all went well, however the instance of the class I create in my main loader movie is still not accessible from within a second SWF that gets loaded into my movie. If I re-instantiate the class in the SWF that is being dynamically loaded, the value reverts to the default value in the class code, not the new value set in my parent (first) movie.

Here is my 开发者_如何学Pythonclass code, stored in an external .as file, is there any way I can simulate the ostype variable being a global variable?

package{
    public class ostype {
        public var ismac:Boolean = false;
        public function returnOs(){
            return ismac;
        }
    }
}

If it helps to know the context of the code, it's used as a simple identifier as to what operating system this flash app is being used on (mac or windows) and as a result let's me server content - such as saving files - in a different manner.

Thanks for any guidance.

Simon


Use static variables:

package {
    class GlobalVars {
        public static var isMac:Boolean = false;
    }
}

You can then access any member like this: GlobalVars.isMac = true;.

Also, you could have a static function returning the OS (although Capabilities.os should do the trick for you).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜