Mediator's onRegister() not running (but only at the first time)

patryk's Avatar


11 Jul, 2011 06:42 AM

Why would the mediator's onRegister() not run at the first time ?

/////////// 1st

var robotView :RobotView = new RobotView( 5, 0xFF0000, 4, 30, 30, 20 );
var robotModel :RobotModel = new RobotModel( robotView, GameObjectType.ROBOT, RobotBodyType.CUBE3, RobotSoundSet.ROBOT1, taskVector );
injector.injectInto( robotModel );
dispatch( new RobotEvent( RobotEvent.CREATE_ROBOT, robotModel ) );

/////////// 2nd

var robotView2 :RobotView = new RobotView( 5, 0xFF0000, 4, 30, 30, 20 );
var robotModel2 :RobotModel = new RobotModel( robotView2, GameObjectType.ROBOT, RobotBodyType.CUBE3, RobotSoundSet.ROBOT1, taskVector2 );
injector.injectInto( robotModel2 );
dispatch( new RobotEvent( RobotEvent.CREATE_ROBOT, robotModel2 ) );

I've tried adding more of those, and all work nicely, apart of the first one. Any ideas ?

  1. Support Staff 1 Posted by Stray on 11 Jul, 2011 07:42 AM

    Stray's Avatar

    Hi Patryk,

    this one is always race conditions - check out the relevant section in the common-problems documentation - it has a list of things to check.

    The nut-shell answer is that your view must be hitting the stage before the mediator is registered. There are 2 strategies for tackling this: 1) delay adding views until all your mediators are registered. 2) map all your mediators so that you map the most deeply nested views first and the context view itself last (if you're mapping it).



  2. 2 Posted by patryk on 11 Jul, 2011 08:14 AM

    patryk's Avatar

    I am mapping the mediator in main context

    public class MainContext extends Context

        public function MainContext( contextView:DisplayObjectContainer )
            super( contextView );
        override public function startup():void
            commandMap.mapEvent( ContextEvent.STARTUP_COMPLETE, StartupCommand, ContextEvent );
            commandMap.mapEvent( GameEvent.NEW_GAME_CLICKED, CreateFloorCommand, GameEvent );
            commandMap.mapEvent( WorldEvent.FLOOR_CREATED, DisplayFloorCommand, WorldEvent );
            commandMap.mapEvent( GameEvent.FLOOR_DISPLAYED, CreateWorldCommand, GameEvent );
            commandMap.mapEvent( WorldEvent.WORLD_CREATED, DisplayWorldCommand, WorldEvent );
            commandMap.mapEvent( RobotEvent.CREATE_ROBOT, CreateRobotCommand, RobotEvent );
            commandMap.mapEvent( RobotEvent.ROBOT_CREATED, DisplayRobotCommand, RobotEvent );
            commandMap.mapEvent( WorldEvent.START_TIME, StartWorldTimeCommand, WorldEvent );
            commandMap.mapEvent( SoundEvent.PLAY_NEXT_TRACK, PlayNextTrackCommand, SoundEvent );
            injector.mapSingletonOf( IWorldModel, WorldModel );
            injector.mapSingletonOf( IKeyboardModel, KeyboardModel );
            injector.mapSingleton( Theme ); 
            injector.mapSingleton( BodyFactory ); 
            injector.mapSingleton( TaskFactory ); 
            mediatorMap.mapView( LayerView, LayerViewMediator );
            mediatorMap.mapView( RobotView, RobotViewMediator );
            mediatorMap.mapView( KeyboardView, KeyboardMediator );
            mediatorMap.mapView( NiceButton, NiceButtonMediator );

    The RobotViews are being added far after the startUpCommand.
    Is there a chance that the RobotViewMediator still has not been mapped even after the startUpCommand?

  3. 3 Posted by patryk on 11 Jul, 2011 09:44 AM

    patryk's Avatar

    Ok , sorted, I am sorry for being a pain :)
    I know I should read the documentation first rather then experiment and guess ... I don't know what's wrong with me...

  4. Ondina D.F. closed this discussion on 01 Nov, 2011 04:14 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? 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