0

I have developed a ios game with 20 levels. I have menu at starting screen which has 20 buttons for 20 levels.

What i need is ,For example If user dont finish level 1 level 2 button should not be enabled ,and so on.as user finish some levels in game the next level should be enabled.

I know its very basic and this is my first game in ios.So please be gentle,Any ideas will be appreciated thanks. :)

Note : my game is fully cocos 2d .any idea in cocos2d is good :)

vishnu
  • 869
  • 1
  • 16
  • 25

5 Answers5

2

In your app delegate you can have a variable that points to what level he finished, and you will also have a function who will enable and disable buttons basing on that variable

To disable a button you can use

button.enabled = NO;

When a user ends a level, incremet the level reached variable and enable/disable buttons

Example using user defaults

setting currentLevel

[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithInt:currentLevel]
                                         forKey:@"level"];
[[NSUserDefaults standardUserDefaults] synchronize];

getting currentLevel

currentLevel = [[[NSUserDefaults standardUserDefaults] valueForKey:@"level"] intValue];
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • yea .am not asking about how to disable button.. i need something like how to keep the data remembered. – vishnu Jul 03 '12 at 12:20
2

you can use NSUserDefaults to remember the level your player has finished.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"savedGameLevel"] ) {
    NSNumber *num = [defaults objectForKey:@"savedGameLevel"];
}

then you can enable/disable the buttons like

 for(UIButton *levelBtn in self.yourView.subviews)
 {
     if(levelBtn.tag == [num intValue] + 1)
         [levelBtn setEnabled:YES];
     else
         [levelBtn setEnabled:NO];

 }

//for saving game state

 -(void)saveLevelAsFinished
   { 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:@"1" forKey:@"savedGameLevel"];

   }
janusfidel
  • 8,036
  • 4
  • 30
  • 53
1

Create all buttons using "custombutton" class.set BOOL variable in that.

@interface CustomLabel : UIButton
{
  BOOL *is_level_completed;

}
@property (nonatomic, retain)BOOL *is_level_completed;

while creating buttons set btn.is_level_completed = NO;After completing every level set btn.is_level_completed = YES;After that check in viewwillappear and change image for completed levels.

to store the data

set tag value for buttons(like 1,2,3...) and while completing the level store the tag value in NSUserDefaults using the following code,

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:btn.tag forKey:@"Completed_level_no"];

in viewWillAppear get the completed level and change the images for buttons.

Madhumitha
  • 3,794
  • 8
  • 30
  • 45
-1

Initially you make every button disable except button1 as follows:

button2.enabled = FALSE;

etc, then user finish 1st level make button2 enabled as follows:

button2.enabled = TRUE;
Nuzhat Zari
  • 3,398
  • 2
  • 24
  • 36
  • yea .am not asking about how to disable button.. i need something like how to keep the data remembered. – vishnu Jul 03 '12 at 12:20
-1

Here is a pseudo code:

for (int i=1; i<=count;i++)
    button[i].enable() //This method will enable the button


for (int i=count+1; i<=20; i++)
    button[i].disable() //This method will disable the button

Basically, the first loop iterates until the count (which is the number of levels reached) and enables the buttons accordingly. Similarly, the second loops starts iterating from the next button and disables the rest.

The second loop can be avoided if all buttons are initiated as disabled.

seedg
  • 21,692
  • 10
  • 42
  • 59
  • yea .am not asking about how to disable button.. i need something like how to keep the data remembered. – vishnu Jul 03 '12 at 12:21
  • Check out the following page http://stackoverflow.com/questions/8040065/storing-data-locally-on-the-iphone on how to store data locally on ios. This should be what you need. – seedg Jul 03 '12 at 12:28