I'm building a puzzle game with 20 levels using Cocos2D. When a player first opens the game, all the levels should be locked except for level 1. When the player passes level 1, then level 2 button should be enabled and so on...
I put the CCButtons in an array so that I could disable unlocked level buttons in a for loop (rather than each individually), but enabling/disabling the object references from the array doesn't work...
For example, this works: _level19.enabled = false;
But this doesn't work: levels[19].enabled = false;
Here is the relevant code block after I initialized CCButton* _level1
to CCButton* _level20
.
-(void) didLoadFromCCB {
// Only set level buttons as enabled if player has unlocked that level
CCButton* levels[20] = {_level1, _level2, _level3, _level4, _level5, _level6, _level7, _level8, _level9,
_level11, _level12, _level13, _level14, _level15, _level16, _level17, _level18, _level19, _level20};
// CCButtons are automatically enabled so disable all buttons that haven't been unlocked
for (NSInteger j = [GameState sharedInstance].levelsUnlocked; j > 20; j++) {
levels[j].enabled = false;
}
}
Any help would be appreciated!
j > 20 seems like a typo. You want to get inside the loop only if your j value is greater than 20 , and then fetch a value from levels array of count 20 which would throw an out of bounds exception.
I am guessing, this should work for you
for (NSInteger j = [GameState sharedInstance].levelsUnlocked; j < 20; j++) {
levels[j].enabled = false;
}