Skip to main content
Participant
February 20, 2009
Question

Help regarding event flow/event listening best practices

  • February 20, 2009
  • 7 replies
  • 586 views
Hi, I'm making a flash memory card game to learn more about to actionscript.

I have:
- a main class that loads on start. This class instantiates a:
- CardLoader class that imports graphical assets and uses these to create all my cards:
- Card class that represents individual card. Can dispatch CardEvent when clicked:
- CardEvent contains info on the Card that dispatched it, such as the cards ID number and the ID of it's matching card.

What I want to do is to have another object to act as a game logic handler.
Example:
- Card 1 was clicked, dispatches CardEvent that ends up with the LogicHandler.
- Card 2 was clicked and also dispatches event.

LogicHandler compares these two cards and does the appropriate thing depending on they being a match or not.


Now, the big questions is, how do I get this event to the LogicHandler?

My custom CardEvent has it's bubbles property set to true. So, I can manage to get such an event all the way back to my main class, in this order: CardEvent > Card > CardLoader > Main.

The problem arises when I want to have my LogicHandler class created by Main. When my CardEvent bubbles back, it does not go through this LogicHandler since it is not part of the event flow on it's way back.

Questions: How do I, following the best possible programming practises, send the event the way I want it to go?

I can only think of one solution myself, being that inside my Main method I set up an eventlistener that listens for this event and passes it on downwards, in this case to the LogicHandler. Sure, it's only a few lines of code in the main method, but what if you have 50 different events in a larger project that needs the same treatment?
Your main method will be a mess by then!

How would you do it?
This topic has been closed for replies.

7 replies

Participant
February 21, 2009
Ok so this is how I currently solved it, let me know if it's incorrect thinking or not, I thought it worked out pretty well.

In main class:
private var logicHandler:LogicHandler;
private var cardLoader:CardLoader;
[following in init method]
cardLoader = new CardLoader();
logicHandler = new LogicHandler();
logicHandler.addDispatcher(cardLoader);

In LogicHandler:
public function addDispatcher(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(CardEvent.CARDCLICK, success);
}

CardLoader creates instances of Card. Inside Card I have this when it is clicked:
dispatchEvent(new CardEvent(CardEvent.CARDCLICK));
Participant
February 21, 2009
Ok let me see if I get this straight:

In my example, I would have to pass a reference of the LogicHandler to my Card class.

In my Card class, I have something like:
logicHandler.addDispatcher(this);
this being the Card of course.

Inside LogicHandler class I have the addDispatcher method that calls the addEventListener method.
So far so good?

The addDispatcher method adds an eventlistener to the Card object, from what I can tell. But it does it inside the LogicHandler, thus the method that is called by the addEventListener can be inside the LogicHandler?

To put it in other words, what's confusing me is that the Listener object adds an EventListener to the dispatching object. Logically for me the listener should add a listener to itself?

The way I first did it in my Card class was:
testevent = new CardEvent(CardEvent.CLICK);
dispatchEvent(testevent);

And then set up the EventListener in the object that I wanted to receive the event.

I'm sorry I don't understand this, despite your obviously good explanations (I think I understand a lot more now, just not the best approach). I will look into the callback functions, sounds a lot interesting as well
February 20, 2009
Read about the pureMVC (AS3) framework. Read its documentation thoroughly (it's very short actually) and you will understand how to construct/design your applications cleanly and make them so that they are easily maintanable.
February 20, 2009
Forget the stage. You don't need to push the events "down" to logic handler. Let the Logichandler "hear" the CardEvent directly.... I've shown you the official AS3 way to do it.
February 20, 2009
OK, look at the example in the link carefully. You see the addDispatcher(yourCard:IEventDispatcher):void {
yourCard.addEventListener(CardEvent.CLICK,
doSomethingHereDude);
}

method? This method should be in your LogicHandler class. This is how you "bind" the card class to the custom event observer.
February 20, 2009
another way is to implement a simple callback function. This is the old trick (from JavaScript) that is used frequently. In general, callbacks are very powerful event handling mechanism that is used in C/C++ and other languages. The problem(and advantage!) with callbacks is they completely decouple the classes. In theory this is good, but in practice it's becomes very hard to read your code if you use a lot of callbacks and don't provide proper documentation. It's sort of like using GOTO statements. If your problem is simple it's not a big deal, but as it grows it becomes a major pain in the butt to decifer your code. You can try using callbacks for fun....
Actually your Loader, URLLoader classes use callbacks internally too....
This is how they are able to receive the response from a socket and act on it (by dispatching a more "formal" AS3 Event)
February 20, 2009
You would need to make the logic handler listen directly to the Card class.
So when someone clicked the card, the card class dispatches a CardEvent....
For that you need to make your LogicHandler implement IEventDispatcher interface and your Card class (or whoever dispatched the CardEvent) must extend EventDispatcher class. (if your card extends MovieClip you are all set 'cause MovieClip extends EventDispatcher by default)...

Look at an example here:
http://www.communitymx.com/content/article.cfm?page=3&cid=76FDB
Participant
February 20, 2009
I think I understand everything but the part of how to make the LogicHandler listen for the event.

Card extends Sprite, so it can dispatch events without a problem. It's just that it travels back to the stage, but then I can't get it from the stage back down again through another branch, in this case the LogicHandler.

Why does the LogicHandler have to implement the IEventDispatcher interface? It sounds like you'd want it to dispatch events by doing so, but I only want it to listen for the event in question.

Thanks for the answer!