Game Development Reference
In-Depth Information
objectForKey:KeyForUnlockedLevel];
return (number ? [number intValue] : 1);
}
The setter method defines the maximum number of levels in the game; this game is inten-
ded to have nine levels. The
currentLevel
property is tested to be in the range 1
through 9 before it is stored in
NSUserDefaults
. The getter method obtains the stored
NSNumber
and either returns its
intValue
or
1
if there is no entry for the key
KeyForUnlockedLevel
yet. It returns
1
because the first level should always be un-
locked.
Listing 8-7
.
Unlocking the next level
-(BOOL) unlockNextLevel
{
int highest = self.highestUnlockedLevel;
if (_currentLevel >= highest)
{
[self setHighestUnlockedLevel:_currentLevel + 1];
}
return (highest < self.highestUnlockedLevel);
}
The currently
highestUnlockedLevel
is obtained from the property setter via
self.highestUnlockedLevel
and assigned to the highest variable. If
cur-
rentLevel
is equal to or greater than the highest number, the
setHighestUn-
lockedLevel
: setter is called so that it unlocks the next level following the current
level. At the end, the method returns a
BOOL
value that indicates whether the next level
was indeed unlocked or not, by comparing the previous highest number with the current
highestUnlockedLevel
.
Highlighting Level Buttons
The level buttons need to be unlocked—that is, their color set to white—if their level is
currently accessible. “Accessible” means that the button's level number is less than or
Search WWH ::
Custom Search