Game Development Reference
In-Depth Information
Last but not least, let's return to the
Scroll View
one more time. When you've played
through the first three levels, you've unlocked the first level of the second world. Yet
when you open the level-selection popover in the
MainScene
, it will always show the first
page. This ought to be fixed.
Open
MainScene.m
, and import the
GameState
header just below the other import, as
shown in
Listing 8-22
.
Listing 8-22
.
Importing GameState
#import "MainScene.h"
#import "GameState.h"
Listing 8-23
.
Switching to the page with the highest unlocked level
-(void) didLoadFromCCB
{
int numPages
= _levelSelectScrollView.numHorizontalPages;
if (numPages > 0)
{
int highest = [GameState
sharedGameState].highestUnlockedLevel;
int worldPage = (highest - 1) / numPages;
worldPage = MIN(worldPage, numPages - 1);
_levelSelectScrollView.horizontalPage = worldPage;
}
}
The
numHorizontalPages
returns
3
in this case, because there are three pages
(worlds). By dividing the
highestUnlockedLevel
minus 1 by the number of pages,
the result is a value in the range of 0 to 3—for instance,
(1 - 1) / 3 = 0
, which is
what happens when you first run the app. Consider that
(3 - 1) / 3 = 0
, so if you
happen to have unlocked all three levels of the first world, the first page (index 0) would
still be shown. Unlocking another level, such as
(4 - 1) / 3 = 1
, will then show the
second page (world).
Search WWH ::
Custom Search