Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
1  
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. –  Byte56 Sep 25 '12 at 18:31
    
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. –  Arthur Wulf White Sep 25 '12 at 23:40
    
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. –  Byte56 Sep 25 '12 at 23:55
    
I don't presume the code is necessary.:) –  Arthur Wulf White Sep 26 '12 at 0:12
add comment

1 Answer

up vote 2 down vote accepted

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
        timeChanged.dispatch(currentTime);
    }

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.

Example:

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();
clock.timeChanged.add(gui.updateTime);

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.

share|improve this answer
    
+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). –  David Gouveia Sep 26 '12 at 8:21
    
@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. –  Arthur Wulf White Sep 26 '12 at 9:42
    
@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). –  bummzack Sep 26 '12 at 11:52
    
Thanks for sharing about AS3-Signals, it a beautiful piece of code –  Arthur Wulf White Sep 26 '12 at 15:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.