Game Development Reference
In-Depth Information
Note Using the generic type id in place of a pointer to a specific Objective-C
class is perfectly legal, and in fact very useful if you don't actually need to do
anything with the particular object except pass it on to other methods.
With the SceneManger methods defined, open GameMenuLayer.m . At the top, be-
low the other #import lines, add the line in Listing 6-12 to make the SceneManager
class known to the GameMenuLayer implementation file.
Listing 6-12 . Adding the SceneManager import
#import "SceneManager.h"
Then add the methods in Listing 6-13 just below the shouldPauseGame method.
Listing 6-13 . Restart and exit buttons simply present a scene
-(void) shouldRestartGame
{
[SceneManager presentGameScene];
}
-(void) shouldExitGame
{
[SceneManager presentMainMenu];
}
Notice that calling the SceneManager class methods does not require having an in-
stance of the SceneManager class. You can just call them by using the class' name it-
self as the receiver. The drawback of class methods is that they do not have access to the
class' stateā€”for instance, they couldn't access any of the SceneManager class' proper-
ties and ivars if it had any.
The SceneManager acts purely as a utility class that runs repetitive, stateless code that
needs to be accessible from various other classes to perform a specific purpose (presenting
scenes). If you have a task that fits this description, you should consider putting it in a
class that has only class methods.
Search WWH ::




Custom Search