title: How do you load a scene while animating a sprite in cocos2d-x?
body: <p>I have a "stage selection" scene and a "game" scene. However when the user presses the button to start the game scene there is a delay between the pressing and the scene showing (about 2 seconds or more on older devices). So what i thought is i should create a loading scene.</p>
  
  <p>So what i am doing right now is passing to my "Loading" scene a <strong><em>std::function</em></strong> which gets called 0.1 seconds after the loading scene appears. This function has the code to start the "game" scene like this:</p>
  
  <p>For creating the loading scene.</p>
  
  <pre><code>auto loading_scene = LoadingScene::createLoadingScene([stage_course]() {
  
      Director::getInstance()-&gt;replaceScene(Game::createScene(stage_course-&gt;course_id));
  
  });
  
  Director::getInstance()-&gt;replaceScene(loading_scene);
  </code></pre>
  
  <p>To load the game scene.</p>
  
  <pre><code>void LoadingScene::onEnter()
  {
      Node::onEnter();
  
      call_after(0.1, callback_start);  
  }
  </code></pre>
  
  <p>The result of this is the loading scene showing with a simple animated sprite of a character running. At first i tried with a delay of 1.0 seconds before the callback to check that the sprite is working correctly (it does, the character runs). But it stops moving when the callback is executed (the one that loads the new scene) and remains like this for about 1-2 seconds until the scene has loaded and then its presented..</p>
  
  <p>Does anyone know how to keep the sprite animated while the scene is getting load so that it never stops running until the "game" scene is shown?</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p><strong>I am using cocos2d-x-3.8.</strong></p>
  
  <p>My loading scene has the following code in its init function to create an animation which is used to animate a spire:</p>
  
  <pre><code>// Create the sprite animation
  Animation *animation = Animation::create();
  for (int i = 0; i &lt; INT16_MAX; i++)
  {
      string frame_sprite_name = StringUtils::format("Interface/loading/0_%d.png",i);
  
      auto frame = SpriteFrameCache::getInstance()-&gt;getSpriteFrameByName(frame_sprite_name);
  
      if (frame) {
          animation-&gt;addSpriteFrame(frame);
      } else {
          break;
      }
  }
  
  animation-&gt;setDelayPerUnit(0.15f);
  
  
  Animate *animate = Animate::create(animation);
  
  
  // Create a temporal sprite to run the animation
  
  auto temp_sprite = Sprite::create();
  
  temp_sprite-&gt;setAnchorPoint(Vec2(0.5,0.5));
  temp_sprite-&gt;setPosition(Vec2(DISPLAY_WIDTH/2.0f,DISPLAY_HEIGHT/2.0f));
  
  
  this-&gt;addChild(temp_sprite);
  
  temp_sprite-&gt;runAction(RepeatForever::create(animate));
  </code></pre>
  
  <hr>
  
  <p>Edit 2:</p>
  
  <p>The reason why my game scene takes too much time to load is because i am loading all the spritemaps my stage needs like this:</p>
  
  <pre><code>// Shared
  
  if (!SpriteFrameCache::getInstance()-&gt;isSpriteFramesWithFileLoaded(STUDENTS_SPRITE_MAP)) {
      SpriteFrameCache::getInstance()-&gt;addSpriteFramesWithFile(STUDENTS_SPRITE_MAP);
  }
  
  if (!SpriteFrameCache::getInstance()-&gt;isSpriteFramesWithFileLoaded(OTHERS_SPRITE_MAP)) {
      SpriteFrameCache::getInstance()-&gt;addSpriteFramesWithFile(OTHERS_SPRITE_MAP);
  }
  
  if (!SpriteFrameCache::getInstance()-&gt;isSpriteFramesWithFileLoaded(INTERFACE_SPRITE_MAP)) {
      SpriteFrameCache::getInstance()-&gt;addSpriteFramesWithFile(INTERFACE_SPRITE_MAP);
  }
  
  if (!SpriteFrameCache::getInstance()-&gt;isSpriteFramesWithFileLoaded(ZOMBIES_SPRITE_MAP)) {
      SpriteFrameCache::getInstance()-&gt;addSpriteFramesWithFile(ZOMBIES_SPRITE_MAP);
  }
  
  if (!SpriteFrameCache::getInstance()-&gt;isSpriteFramesWithFileLoaded(PORTRAITS_SPRITE_MAP)) {
      SpriteFrameCache::getInstance()-&gt;addSpriteFramesWithFile(PORTRAITS_SPRITE_MAP);
  }
  
  if (!SpriteFrameCache::getInstance()-&gt;isSpriteFramesWithFileLoaded(CUTS_SPRITE_MAP)) {
      SpriteFrameCache::getInstance()-&gt;addSpriteFramesWithFile(CUTS_SPRITE_MAP);
  }
  
  // Exclusive
  
  if (!SpriteFrameCache::getInstance()-&gt;isSpriteFramesWithFileLoaded(TEACHERS_SPRITE_MAP)) {
      SpriteFrameCache::getInstance()-&gt;addSpriteFramesWithFile(TEACHERS_SPRITE_MAP);
  }
  </code></pre>