4

I was told recently by a Flash developer that I respect that using

include "functions_file.as";

is not the appropriate way to import a list of functions into an AS3, Flash CS5 document.

We're not talking about objects and packages, just a list of functions.

So what IS the best way to do this?

jerebear
  • 6,503
  • 4
  • 31
  • 38
  • @poke, static classes make sense in a strictly-typed OOP paradigm, but in ActionScript (and more specifically ECMAScript) a namespaced function is essentially equivalent. The static class is, essentially, replaced by a package in this case. – zzzzBov Mar 14 '11 at 21:54

2 Answers2

2

Your miscellaneous functions belong in their own individual files just like your classes:

package foo.bar
{
  public function baz(fizz, buzz)
  {
    return fizz * buzz + 7;
  }
}

And then use the import directive just like you would for a class:

import foo.bar.baz;

The important difference is that the include directive will execute the file every time it's called, which will essentially be re-writing the functions each time it's called.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • What does the file name/structure look like? – jerebear Mar 14 '11 at 21:50
  • the same as it would if you wrote a class: `/foo/bar/baz.as` – zzzzBov Mar 14 '11 at 21:51
  • Can can you still access the function by calling baz(); directly? – jerebear Mar 14 '11 at 21:58
  • Actually, from your example, it looks like only one function can be in that class. I have several function libraries I'm trying to package up. – jerebear Mar 14 '11 at 22:05
  • You'll need to call `import foo.bar.baz;` before being able to directly call `baz()`. If you have imported another function of the same name (different package): `fizz.buzz.baz` you'll need to use the fully qualified names to call either: `foo.bar.baz(); fizz.buzz.baz();` – zzzzBov Mar 14 '11 at 22:05
  • @jerebear, the example contains no classes. It sounds to me like you've never used OOP AS with classes in external files. If you want multiple functions in the same **package**, you just need to specify the same **package**. I will reiterate that *your miscellaneous functions belong in their own individual files*. – zzzzBov Mar 14 '11 at 22:08
  • I guess that must be it. Is there a way to package up a group of functions into one file without using "include" ? – jerebear Mar 14 '11 at 22:13
  • Is there a reason all your functions need to be in one file? They'll all have to be in the same directory anyway. You *could* put them in a static class, but then you'd need to call `StaticClass.foo()`, and `StaticClass.bar()` instead of `foo()`, `bar()`. If it's just because you want to import all the functions with one line, you can use `import foo.bar.*` to add the entire package of functions. – zzzzBov Mar 14 '11 at 22:16
  • And that's what I'm trying to do. I have several different function libraries. Each function has one purpose. I have my file set up as /com/expo/booths.as and the in my code import com.expo.* and none of the functions are coming through. – jerebear Mar 14 '11 at 22:17
  • did you link the library to the fla file (or is it in the same directory as the .fla file)? Did you make sure to specify the package correctly in each file? – zzzzBov Mar 14 '11 at 22:59
  • I think I've realized what I'm missing here. Thank you all for your help! – jerebear Mar 15 '11 at 03:38
0

Generally, in 99.9% of situations, I'd agree.

I'm going to be a contrarian though, and say that there is at least one valid use case for this in that you can specify a set of behaviors to happen on an arbitrary frame in many different library symbols without those symbols necessarily needing to define their own class. I'd say the limit to this would be something to the effect of:

import complete.as

// inside complete.as
this.dispatchEvent(new Event(Event.COMPLETE, true));
this.stop();

//  Note : dispatchEvent and stop are 
//  the only function calls I'd feel 
//  comfortable putting on a keyframe script.

This is especially handy if the clips in question don't need to (or cannot) share a relationship in an inheritance tree, but they all require a certain specific set of code to execute on a given frame. If you keep that code in a separate .as file, and then need to change it later (maybe to add or remove that dispatch call), you don't have to track down every library symbol and modify them manually. Just modify the included .as file.

scriptocalypse
  • 4,942
  • 2
  • 29
  • 41
  • I find the `include` directive to also be useful when you want to add runtime extensions to flash classes via prototype: `if (!Array.prototype.foo) Array.prototype.foo=function(){...};` Unfortunately some of the basic classes are locked down as `final` which prevents runtime extension. I've wished I could add a couple functions to `Number`, instead I have to write wrappers. – zzzzBov Mar 14 '11 at 22:03