Question
Help regarding event flow/event listening best practices
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?
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?