1

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 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Jonathan, what's the purpose of "contains" above?
It's just an example function... yours can be anything. As long as you set the type to static, you can access it anywhere without instantiating an object.
Oh, I thought "contains" was an AS3 keyword, but now I realize it's the name you chose for the function. Silly me!
0

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

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

1 Comment

@ggkmath It's an old fashioned/not object oriented approach. It will obscure your code and make it more diffucult to read and to maintain. I would recommend using Jonathan's solution if it is feasible for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.