Flash The Ripper

Life Around Technology

How to define your own global function in as3

It is very easy. However, I’ve found no info on this topic in the web. I wonder why? First answer: everyone knows how to define the top-level function in AS3 so no-one even drops a line about such obvious thing. Second answer: my search strategy is wrong. Anyway, the approach to your own global functions in Flash9 is really simple, just two steps for those who still miss this feature.

To define your own top-level function (for example, “myTrace“) in AS3:

1. Create the file named “myTrace.as” in the root of your classpath directory.
2. Add the following code and save it:

package {
	public static function myTrace(strToTrace:String)
		trace("myTrace says: " + strToTrace);

3. That’s all.
Now your myTrace function is the top-level one. It is visible from ANY of your AS3 classes with NO imports. No hacks. You can call it “global method in AS3″ or “top-level function in AS3″ or whatever you like. This is amazing and I wonder why no one even blogged a line about such simple and great solution except the Miramax at as3.ru. Am I missing something evident here? Feeling like complete n00b.