ActionScript 3: How to use Event Listeners to Call Functions with Parameters

By default, you can’t. But there’s a way around it so you can by creating a custom event class! In this case, I’m going to deal with extending the Timer class to create a custom timer class with additional properties that we can use to pass data to functions.

The Problem

To demonstrate the problem, consider the following simple timer code:

This will work just fine – it’ll call the greet function once to output “Hello!” three seconds after the timer was started. But what if I wanted to say “Hello, SOME_NAME_STRING”, like “Hello, Bob!”, well you might think you could just do something like this:

But you CAN’T! ActionScript 3 doesn’t allow you to pass parameters other than the TIMER event (i.e. the timer going off) to functions which are bound to a timer. And even changing the order of the parameters, or using a line such as the one below simply won’t work:

The Solution

Now, there are things you can do like use intermediate functions which might call a second function, or using anonymous in-line functions (there’s a good forum thread on it here if you want to investigate that route) – but there’s a better way of doing it: We can just create our own custom Timer class!

For this simple text example we could use something like this:

Custom Timer Class (String version) Code:

And then we could use our custom timer like this:

Flash File Code:

This will print out “Hello, Bob!” just fine using the additional property in our custom timer class! :)

Now that’s all well and good, but most people want to modify some manner of DisplayObject (Sprite, MovieClip etc.) when the timer fires, so for that we can just modify our custom timer class to expect a DisplayObject like this:

Custom Timer Class (DisplayObject version) Code:

Flash File Code:

Note: The above code assumes you’ve created a symbol with the class name “Circle” – to do so just draw a circle on the stage, stab F8 to convert it to a symbol, and check the Export for ActionScript checkbox and enter Circle as the class name before hitting the [OK] button.

Just like our String example, adding the additional property to our custom timer class allows us to bind the timer to a specific object which we can then manipulate from the function called when the timer fires! So in this case, two seconds after the timer starts our fadeIn function will be called and our circle instance will have its alpha property set to 1 (so it’s fully opaque – hence visible!).

Wrap Up

I came up against this problem whilst helping out some of my ActionScript students who are first-time coders, so wanted to code more Procedurally than Object-Oriented. When you use OOP there are better ways of going about things, but this is still a useful trick to know when you’re dealing with Event Listeners and Timers. I hope it helps out anyone who’s been banging their head against this problem! Cheers!

One thought on “ActionScript 3: How to use Event Listeners to Call Functions with Parameters”

Leave a Reply

Your email address will not be published.