Global functions in AS3

Posted by
Ziwei

Posted On
Jun 23 2009 2:31 am

Setting a global function that’s visible to any classes in your project without any form of import is easy and straightforward with AS3.

First, let us create a AS file called “Global” with the following code

  1.  
  2. package {
  3. public class Global {
  4. public static function performAction():void {
  5. trace("Called global function")
  6. }
  7. }
  8. }

Now save this file into the document root of your working file or the folder root of any classpath that you defined in the actionscript 3.0 setting under publish settings.
Voila, now you are able to call the function anywhere by calling Global.performAction().

Comments ( 2 )

  1. December 11th, 09 5:03 pm Tea

    I’m sorry but the name and content in this article are misleading.

    Creating a class named ‘Global’ and accessing a static function thereof doesn’t give you a true global function.

    Global functions do not have many benefits. Surely static functions are often better, but I think this article will confuse many developers. Maybe you can improve it?

  2. December 11th, 09 5:31 pm Ziwei

    Rather than creating a true global function, the aim of this article is simply to provide a way to recreate the _global object in AS2 without any importing of classes in AS3. The benefits has more got to do with the programming habits of AS2 coder who are trying to migrate to AS3 than real performance gain.

Post Comment

Remember! Spamming is for losers

Trackback from your own website