IBM: Six Ways to write more comprehensive code →← AS3: addFrameScript and BigMovieClip

AS3: Notes on arguments…

I keep forgetting how to handle arguments in AS3. If you haven’t started to migrate yet, you should be aware that handling arguments is among the more frustrating aspects. Arguments are much stricter in AS3, which is a blessing and a curse. A blessing because you have far more control over what a method will accept (and as a result smarter, cleaner code). And a curse because… well, I already said that.

Here are a couple of quick hints on arguments that I find I need to keep reminding myself about:

overloading

If you need a function to handle a variable number of arguments, use a rest (…).:


public function onFoo(... fooArgs):void
{
for(var i:int=0;i<fooArgs.length;i++) { trace(fooArgs[i]); }
};

Here’s some more on overloading.

default variables

AS3 will throw an error if you attempt to pass null for an argument where it expects one. That is, unless you define a default variable.


public function onFoo($myNumber:Number = 1):void
{
};

or this example, while would let you call the method an event would normally fire without the event.


public function onSomeEvent($evt:Event = null):void
{
};

*

* lets you define anything as the DataType for a variable. Handy.


public function onFoo($myArg:*):void
{
};

May 30th, 2007  by Stephen  /  2 Comments

Comments on “AS3: Notes on arguments…”

  1. good to know. thanks!

    felix on June 4th, 2007 at 3:55 pm
  2. Thank you. “incorrect number of argument” was a pain in the ass for me until now. Now I can be more casual with my programming :)

    GameReclaim on August 13th, 2008 at 5:18 am