I am doing a refresher on ActionScript 3.

Other than using rudimentary tools like, Event.ENTER_FRAME and using events to receive input from the user's mouse and keyboard, I find that I rarely use events and prefer to use public functions.

I am starting to reconsider this approach. From what I have learned thus far, the event system works this way:

Each dispatcher can listen to itself during the target phase. Each display object on the stage, can listen to it's children before the target phase, during Capture or after the target phase, during bubbling.

  1. I would like to see an example of good use of the Event class for game programming. Have you used Event driven programming in a game and have a good example you could share?

  2. A lot of times I want to pass a message to a 'sibling' so I could have their methods listen to the stage or the parent object. Is that the best way to communicate between objects that are not parents of the dispatcher they want to listen to?

  3. I would like to know if events are better performance wise than public functions. Are there cases where using events can result in superior performance than avoiding events and sticking to function calls?

  • 1
    \$\begingroup\$ What's the point of the code? You typically want to keep you questions as clean and to the point as possible. Think about how you can make it as easy as possible to answer. \$\endgroup\$
    – House
    Sep 25, 2012 at 18:31
  • \$\begingroup\$ This is an example of how I understand ActionScript 3 event flow (which may be) helpful in pointing me in the right direction (maybe I'm missing something) or just help make sure we are all on the same page. \$\endgroup\$
    – AturSams
    Sep 25, 2012 at 23:40
  • \$\begingroup\$ OK, it may just come across as an intimidating wall of code. People are less likely to answer in such instances. If you could sum it up as code snippets that would probably be better. I'm not saying you're doing things wrong, I'm just trying to help you get help faster. \$\endgroup\$
    – House
    Sep 25, 2012 at 23:55
  • \$\begingroup\$ I don't presume the code is necessary.:) \$\endgroup\$
    – AturSams
    Sep 26, 2012 at 0:12

1 Answer 1


The native Flash Events such as ENTER_FRAME etc. are quite fast and there's no way around them. If you want to learn more about the event flow, read this article. Personally I try to avoid the event system whenever possible though. Custom events (subclasses of Event) are slow. Using events can also cause memory issues when not used carefully (because a pointer to your object will remain in the event-dispatcher and therefore the memory will never be freed by the GC, even when the object is no longer needed).

Still, events are a great way to decouple components. Using public functions and calling them directly is of course much faster, but it means a tight coupling. In an event driven system, you fire an event and be done with it. You don't have to know anything about who receives the event and what kind of function you have to call.

Instead of events, I prefer a system called "signals and slots". There's a great implementation of that pattern in actionscript, called as3-signals. There's also the (slightly) faster but less versatile turbosignals.

So how does this work. Consider the following example (using as3-signals, unnecessary code bits stripped away):

class GameClock
    public var timeChanged:Signal = new Signal(int);

    private function update():void {
        // time has changed, dispatch signal

The GameClock class has a public property "timeChanged" which is a Signal. The "int" parameter specifies one parameter of type int. In an update method, I can now dispatch the current time.

As you can see, this is a loosely coupled system.. we don't call any public methods of other objects, just dispatch the signal (much like dispatching an event) and be done with it.

Now any other component can be connected to this signal, as long as it has a "slot" that fits, that means a function that matches the signature of the signal.


class GameGui
    public function updateTime(time:int):void {
        // update the GUI with the current time

To connect the GameGui to the GameClock, you would do the following (maybe in your game start-up method)

var gui:GameGui = new GameGui();
var clock:GameClock = new GameClock();

You can connect to as many components as you want to listen for this signal. Eg. you could add a component that looks for remaining game-time and plays a warning sound if 10 seconds remain, etc.

So why did I propose Signals/Slots instead of public functions or Events? The Signals/Slots pattern is really powerful, because it is almost like calling public functions directly, but it allows for loose coupling of components. Another benefit is, that you can see the type of signals a class dispatches by looking at its interface. You don't have that with events. They aren't declared anywhere and any bit of code could fire an event... therefore events need much better documentation to be understandable.

  • \$\begingroup\$ +1 This is a very well written answer. However, I'm not sure that I fully understand what the difference between a signal and an event is. Are you talking specifically about the Actionscript event syntax? Because I've converted your signal example to C# using events, and the result is identical (check the code here). \$\endgroup\$ Sep 26, 2012 at 8:21
  • \$\begingroup\$ @David Gouveia, the questioned was aimed at the W3C recommendation for dealing with events implemented in AS3. There code would look like this: first we wouldn't declare the timeChanged signl, we would instead declare a public static string TIME_CHANGED = "timeChanged" and then we would subscribe to the instance of the class itself, clock.addEventListener(TIME_CHANGED, gui.updateTime); //However, as he suggested, I cannot control the function parameter type, I am limited to using en event as a parameter. \$\endgroup\$
    – AturSams
    Sep 26, 2012 at 9:42
  • \$\begingroup\$ @DavidGouveia Thanks. Events and signals are similar, but not identical in AS3. Probably the biggest difference is what Arthur Wulf White pointed out, that an event always passes an event object to the event listener methods. For every custom event type and parameter combination you would need a separate Event class... it's also quite an overhead to create all these Event objects. The signals implementation just uses function pointers and calls them directly (without the need of event objects). \$\endgroup\$
    – bummzack
    Sep 26, 2012 at 11:52
  • \$\begingroup\$ Thanks for sharing about AS3-Signals, it a beautiful piece of code \$\endgroup\$
    – AturSams
    Sep 26, 2012 at 15:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .