Game Development Reference
In-Depth Information
Add the method in Listing 8-10 just below the shouldClose method in
MainMenuLevelSelect.m .
Listing 8-10 . Loading a level...or not
-(void) shouldLoadLevel:(CCButton*)sender
{
GameState* gameState = [GameState sharedGameState];
int levelNumber = sender.name.intValue;
if (levelNumber <= gameState.highestUnlockedLevel)
{
gameState.currentLevel = levelNumber;
[SceneManager presentGameScene];
}
else
{
// maybe play a 'access denied' sound effect here
}
}
As before, the button's name property is converted to int via the intValue property of
NSString . The only difference here is that the button is passed to the method as the
sender object. So depending on which level button the user taps, the sender will be the
button that was tapped.
If the levelNumber obtained from the button's name is less than or equal to the
highestUnlockedLevel , the user can play that particular level. At this point, the
GameState singleton's currentLevel property is updated so that it is readily avail-
able from within the SceneManager . SceneManager then presents the GameScene
with the current level—which is a method that you'll update shortly. And if the user
tapped a level button whose level is currently locked, the shouldLoadLevel: method
simply ignores the request.
Now open SceneManager.m . First import the GameScene and GameState headers.
Add them at the top of the file where the other import line is. See Listing 8-11 .
Listing 8-11 . Importing the GameScene header
#import "SceneManager.h"
Search WWH ::




Custom Search