How to wait until all Mediators are instanciated?
First of all I want to say, that I'm doing my first steps with Robotlegs. I've read the Robotlegs book and studied the repository documentation of RL2. For testing and evaluation purpose I've built a simple game similar to Simon with four buttons added dynamically to a group.
Here is an overview of the application flow for the problem.
-
The GameMediator sends out an event to retrieve the buttons stored in a model
dispatch(new GameEvent(GameEvent.RETRIEVE_ALL_BUTTONS));
-
The registered RetrieveAllMusicButtonsCommand gets the buttons from the model and dispatches an own add event for every button. Afterwards the game starts with first turn of simon.
public class RetrieveAllMusicButtonsCommand extends BaseActor {
[Inject] public var buttons:MusicButtonModel; public function execute():void { for each(var vo:MusicButtonVO in buttons.buttons.source) { dispatch(new MusicButtonEvent(MusicButtonEvent.ADD, vo)); } dispatch(new GameEvent(GameEvent.PLAY_SIMONS_TURN)); }
}
The problem now is, that the GameEvent is dispatched before the MusicButton.mxml is binded to the MusicButtonMediator and can listen to the first action.
I've created the workaround that the GameEvent is dispatched after 100 ms so that the application has a little bit time to create and bind the mediators.
var timerId:uint = setTimeout(function():void {
clearTimeout(timerId);
dispatch(new GameEvent(GameEvent.PLAY_SIMONS_TURN));
}, 100);
In my opinion this is not the correct and clean solution, because the time may be different on every system the game is running. So I'm looking for a solution to wait until all mediators are created and bind to their view. Do you know a better solution?
Any help is very appreciated.
Comments are currently closed for this discussion. You can start a new one.
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
1 Posted by krasimir on Feb 16, 2012 @ 03:31 PM
Hello,
you should avoid such a setTimeout solutions. Try to put your entry points in onRegister method of your mediators. I'm not sure that I understood your problem, but that
The GameMediator sends out an event to retrieve the buttons stored in a model
dispatch(new GameEvent(GameEvent.RETRIEVE_ALL_BUTTONS));
should be done in the onRegister method of GameMediator . And before to make that dispatch add your listeners.
2 Posted by Holger on Feb 16, 2012 @ 03:55 PM
Thanks for your answer. I forgot to write that the GameEvent.RETRIEVE_ALL_BUTTONS is already dispatched in the onRegister/initialize method of the GameMediator.
The
dispatch(new GameEvent(GameEvent.PLAY_SIMONS_TURN));
runs an other PlaySimonsTurnCommand that pushes the MusicButton by sending an event. The corresponding MusicButtonMediator is listen to the contextaddContextListener(MusicButtonEvent.PUSH, pushButton, MusicButtonEvent);
for that Event.But as I wrote before, the MusicButtonMediator is created to late so the MusicButtonEvent.PUSH from Simons turn has no effect.
So I think the best solution may be to wait/listen until all MusicButtonMediators are instanciated and than doing the first turn.
3 Posted by krasimir on Feb 16, 2012 @ 04:12 PM
So, as far as I understood you need to catch an event in a mediator which is still not initialized, because its view is not added on the stage. What if you catch that event in a command then add the view and then dispatch another event.
4 Posted by Holger on Feb 17, 2012 @ 10:22 AM
Good point. I thought it's not the best way to add an context event listener in a command.
I've extended the BaseActor with an addContextListener and removeContextListener method, dispatch an event in the MusicButtonMediator.initialize() method and catch this event by the RetrieveAllMusicButtonsCommand. If all mediators have dispatched their events the counter is zero and the GameEvent is dispatched for further actions.
So here is my current solution:
Do you have any tip how to handle/structure all the events and their listeners? I mean, the events can be dispatched from nearly every class of the framework. And (context) event listener can be defined in the config class, mediators and commands. So in a larger project it will be very difficult to watch the application flow and know the places where an event comes from and goes to.
Ondina D.F. closed this discussion on Apr 19, 2012 @ 02:30 PM.