tutorials flash globalas3
AS3’s global
As we can see on the ActionScript 2.0 Migration page of macromedia’s (Adobe) livedocs, the _global object is removed in the native AS3 framework. The great innovation (compared to AS2) comes from the fact that it is possible to code outside the packages and classes in an .as file. It is possible to declare global variables all over the place in AS3 files.
In AS2 we were obliged to use a “hack” for launching global actions with the static keyword, for example:
// Code AS2____ class MyClass { // ----o Constructor function MyClass() {} // ----o Init Broadcaster static public var INITBroadcaster = AsBroadCaster.initialize(MyClass.prototype) ; }
It is now possible to code directly in the file without using a static variable, a bit like on the timeline of Flash. In the end we are closer to SSAS than to AS2 in AS3 files. The only thing is is that it is possible at any time to declare classes and packages! I really hope that the next version of Flash Server Media will follow this new ECMAScript standard by directing SSAS towards AS3 or Javascript 2!
Now, after the joy of this new discovery, a question poses itself!! How can we easily achieve global scope in AS3?
It is obvious that anything that we declare outside of a class becomes global… but how following this can we enumerate for example all that is contained in the global scope? Or what do we do to dynamically add properties to this object?
I really stressed about this… while testing a little in all directions to the ends of the code to find the AS3 “global”. Was it therefore impossible to target the global scope in AS3? I suddenly felt ill at ease… I could not imagine a ECMAScript based language not having any reference of the global object!
Fortunately, there was a small discussion with Zwetan on GoogleTalk and consequently some links about AS3 like: AS3 programming 101 for C/C++ coders…
In the end there was a breakthrough by Zwetan and some tests… and hey presto, it is possible to easily reach the global scope of the application in AS3! In fact, the answer was obvious; virtually identical to what we can do in SSAS… we just needed to really carefully determine the use of the global variables in AS3!!
Solution
package { import flash.display.Sprite; import flash.utils.trace ; public class MainClass extends Sprite { // ----o Constructor public function MainClass() { trace(">> _global : " + _global) ; trace(">> myGlobalProperty : " + _global.myGlobalProperty) ; } } } if (_global == null ) { var _global = this ; } _global.myGlobalProperty = "hello world" ; trace(">>> myGlobalProperty in global namespace" + _global.myGlobalProperty) ;
We can therefore directly code a _global object in the AS3 source files, a little bit like on a timeline in Flash with the _root. Using this method, we preserve the flexibility of SSAS and the robustness of a class-based language.
In the end, I choose the declaration of this _global object via an include when I really need it.
EKAMELEON - ALCARAZ Marc (2006). You can find this tutorial and comments on the subject on my blog.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


