Game Development Reference
In-Depth Information
alternative would be to register a node with NSNotificationCenter and, of course,
properly unregister it as well, which is more cumbersome and error prone than just imple-
menting a specific UIApplicationDelegate selector.
An example use is to forward the applicationWillResignActive: message to
any node interested in handling that particular message. Add the code in Listing 6-15 to
AppDelegate.m just below the newly added performSelect-
or:onNode:withObject:recursive: method.
Listing 6-15 . Forwarding a UIApplicationDelegate message to all nodes in the scene
-(void) applicationWillResignActive:(UIApplication
*)application
{
CCScene* scene = [CCDirector
sharedDirector].runningScene;
[self performSelector:_cmd
onNode:scene
withObject:application
recursive:YES];
[super applicationWillResignActive:application];
}
The currently running scene obtained from CCDirector runs the _cmd selector on the
scene and recursively on all of its child nodes, passing along the application object.
The CCDirector class is the Cocos2D UIViewController subclass and manager
of Cocos2D application states.
What's the _cmd selector here, and where does it come from?
It's a hidden method parameter every Objective-C method receives behind the scenes. It is
the selector of the currently running method. In other words, in this example _cmd is
identical to writing @selector(applicationWillResignActive:) . The other
implicit Objective-C method parameter is one you've already made frequent use of: it's
the self keyword.
To actually pause the game when the application will resign, add the method in Listing
6-16 to GameMenuLayer.m , perhaps just below the already existing
shouldPauseGame method since it doesn't do anything but run the
shouldPauseGame method.
Search WWH ::




Custom Search