1
votes

I'm using Flash Builder 4.5. In an .mxml file, if you want to use an actionscript function residing in an external .as file, one simply adds to the .mxml file:

<fx:Script source="filename.as"/>

where filename.as contains the actionscript function(s).

Is there a way to do a similar thing from an .as project file? That is, I have an actionscript project created in Flash Builder 4.5 (e.g. no .mxml files anywhere), and I have a nested actionscript function. How can I place that nested actionscript function in an external file, then call it from the original file? My goal is to have that nested actionscript function be called from multiple other files, so I don't have to nest it multiple times.

2

2 Answers

2
votes

The easiest way to do this is with a static function. Create an AS class file and remove the constructor. Now place your function in it and set the type to "public static", here is an example of a simple "stirng utility" class.

package some.place.inyourapp
{   
  public class StringUtils 
  {

    public static function contains( string:String, searchStr:String ):Boolean
    {
        var regExp:RegExp = new RegExp( searchStr, "i" );           
        return regExp.test( string );           
    }
  }
}

Now you can call that function without instantiating the class anywhere in your app by saying:

StringUtils.contains("this is cool","cool")

Of course, StringUtils must be imported

import some.place.inyourapp.StringUtils

0
votes

Although it is not recommended, you could use the include operator.

...
include "myFile.as";
...