Game Development Reference
In-Depth Information
{
// Decide which scene to load based on the TargetScenes enum.
switch (targetScene)
{
case TargetSceneFirstScene:
[[CCDirector sharedDirector] replaceScene:[FirstLayer scene]];
break;
case TargetSceneOtherScene:
[[CCDirector sharedDirector] replaceScene:[SecondLayer scene]];
break;
default:
// Always warn if an unspecified enum value was used
NSAssert2(nil, @"%@: unsupported TargetScene %i", ←
NSStringFromSelector(_cmd), targetScene);
break;
}
}
Because the LoadingScene is derived from CCScene and requires a new parameter
passed to it, it's no longer sufficient to call [CCScene node] . The
sceneWithTargetScene method first allocates self , calls the initWithTar-
getScene method, and returns the new instance. The sceneWithTargetScene
class method isn't strictly necessary but is good style. It makes the calling code shorter
because you can avoid calling alloc . Also, most cocos2d and Cocoa classes have
such initializing class methods, and many developers expect classes to offer them.
The init method of the LoadingScene class simply stores the target scene in a
member variable, creates the “Loading…” label, and calls scheduleOnce with a
0.0f delay to ensure the loadScene method is called one frame after init .
Caution Why not just call replaceScene right inside the init method?
Two reasons. Reason number 1 is never call CCDirector 's replaceScene
in a node's init method. It will cause a crash. The Director can't cope
with replacing a scene from a node that's currently being initialized. Reason
number 2 is that you need to give the LoadingScene class time to draw it-
Search Nedrilad ::




Custom Search