Game Development Reference
In-Depth Information
What just happened?
We have created a
Scene
transition with a
Layer
class that never gets destroyed, so we
don't have to instantiate new platform and gravity switch sprites with each new level.
There are, of course, problems and limitations with this process. We cannot transition
between the two
GameLayer
objects, for instance, as we only ever have one of these ob-
jects.
There are also some special considerations when leaving
GameLayer
and when getting
back to it. For instance, we must make sure we have our main loop running when we get
back to
GameLayer
.
The only way to do that is by unscheduling it whenever leaving
GameLayer
and schedul-
ing it again when returning, as follows:
//when leaving
unscheduleUpdate();
auto newScene = TransitionMoveInL::create(0.2f,
MenuLayer::scene());
Director::sharedDirector()->replaceScene(newScene);
//when returning
_instance->scheduleUpdate();
Tip
Again, architecturally speaking, there are even better options. Possibly the best one is creat-
ing your own game elements cache, or game manager, with object pools and everything
that needs instantiating stored inside it. And then have this cache be a singleton that every
scene can access. This is also the best way to share game-relevant data between scenes.