@@ -67,6 +67,7 @@ static const char* kJoinButtonImage = "join_game.png";
6767static const char * kLoginButtonImage = " login.png" ;
6868static const char * kLogoutButtonImage = " logout.png" ;
6969static const char * kSignUpButtonImage = " sign_up.png" ;
70+ static const char * kLoadingBackgroundImage = " loading_background.png" ;
7071
7172// Regex that will validate if the email entered is a valid email pattern.
7273const std::regex email_pattern (" (\\ w+)(\\ .|_)?(\\ w*)@(\\ w+)(\\ .(\\ w+))+" );
@@ -89,6 +90,10 @@ bool MainMenuScene::init() {
8990 // Initializes the firebase features.
9091 this ->InitializeFirebase ();
9192
93+ // Initializes the loading layer by setting the background that expires on a
94+ // delay.
95+ this ->InitializeLoadingLayer ();
96+ 9297 // Initializes the authentication layer by creating the background and placing
9398 // all required cocos2d components.
9499 this ->InitializeAuthenticationLayer ();
@@ -637,6 +642,29 @@ void MainMenuScene::InitializeLoginLayer() {
637642 });
638643}
639644
645+ // Creates and places the loading background. Creates a action sequence to delay
646+ // then swap to the authentication state.
647+ void MainMenuScene::InitializeLoadingLayer () {
648+ // Creates the delay action.
649+ auto loading_delay = DelayTime::create (/* delay_durration*/ 2 .0f );
650+ 651+ // Creates a callback function to swap state to kAuthMenuState.
652+ auto SwapToAuthState =
653+ CallFunc::create ([this ]() { state_ = kAuthMenuState ; });
654+ 655+ // Runs the sequence that will delay followed by the swap state callback
656+ // function.
657+ this ->runAction (Sequence::create (loading_delay, SwapToAuthState, NULL ));
658+ 659+ // Creates the loading background sprite.
660+ const auto window_size = Director::getInstance ()->getWinSize ();
661+ const auto background = Sprite::create (kLoadingBackgroundImage );
662+ background->setContentSize (window_size);
663+ background->setAnchorPoint (Vec2 (0 , 0 ));
664+ loading_background_ = background;
665+ this ->addChild (loading_background_);
666+ }
667+ 640668// 1. Creates the background node.
641669// 2. Adds the layer title label: authentication.
642670// 3. Adds the id and password text fields and their event listeners.
@@ -843,11 +871,11 @@ void MainMenuScene::update(float /*delta*/) {
843871 assert (0 );
844872 }
845873}
846- // Returns kAuthMenuState. This will be the default update method and
847- // immediately swap to auth state. TODO(grantpostma): have this display a
848- // loading screen before swapping.
874+ // Returns kInitializingState. Waits for the delay action sequence to callback
875+ // SwaptoAuthState() to set state_ = kAuthMenuState.
849876MainMenuScene::kSceneState MainMenuScene::UpdateInitialize () {
850- return kAuthMenuState ;
877+ this ->UpdateLayer (state_);
878+ return kInitializingState ;
851879}
852880
853881// Updates the layer and returns the kAuthMenuState.
@@ -964,4 +992,5 @@ void MainMenuScene::UpdateLayer(MainMenuScene::kSceneState state) {
964992 auth_background_->setVisible (state == kAuthMenuState );
965993 login_background_->setVisible (state == kLoginState );
966994 sign_up_background_->setVisible (state == kSignUpState );
995+ loading_background_->setVisible (state == kInitializingState );
967996}
0 commit comments