2014-01-31 151 views
-3

我正在創建一個簡單的遊戲應用程序。 當用戶點擊第一個按鈕並且int click爲0時,他可以點擊第二個按鈕,依此類推。 如果用戶首先點擊第二個按鈕,然後點擊第一個按鈕,他的生命將會下降。 當用戶達到0生命並且查看返回到第一頁時,我遇到了問題。 然後當我點擊按鈕再次玩,我有0生命,不要3!簡單遊戲的NSUserDefault

這裏是我的代碼:

@implementation livello1 

    int click=0; 
    int lifes=3; 

-(void)updateLifes:(int)lifes{ 

    if (lifes==1){ 
     NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
     [standardDefaults setObject:@"1" forKey:@"UserLifes"]; 
     [standardDefaults synchronize]; 
     [self showLifes]; 
     livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil]; 
     livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self presentViewController:livello1view animated:YES completion:nil]; 
    } 
    else if (lifes==2){ 
     NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
     [standardDefaults setObject:@"2" forKey:@"UserLifes"]; 
     [standardDefaults synchronize]; 
     [self showLifes]; 
     livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil]; 
     livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self presentViewController:livello1view animated:YES completion:nil]; 
    } 
    else if (lifes==3){ 
     NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
     [standardDefaults setObject:@"3" forKey:@"UserLifes"]; 
     [standardDefaults synchronize]; 
     [self showLifes]; 
     livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil]; 
     livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self presentViewController:livello1view animated:YES completion:nil]; 
     } 
    else if (lifes==0){ 
     NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
     lifes=3; 
     [self showLifes]; 
     [standardDefaults setObject:@"3" forKey:@"UserLifes"]; 
     [standardDefaults synchronize]; 
     gioca *giocaview = [[gioca alloc] initWithNibName:nil bundle:nil]; 
     giocaview.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self presentViewController:giocaview animated:YES completion:nil]; 
    } 
} 

-(void)showLifes{ 
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
    if ([[standardDefaults stringForKey:@"Userlifes"] isEqualToString:@"1"]) { 
     lifes=1;} 
    if ([[standardDefaults stringForKey:@"Userlifes"] isEqualToString:@"2"]) { 
     lifes=2;} 
    if ([[standardDefaults stringForKey:@"Userlifes"] isEqualToString:@"3"]) { 
     lifes=3;} 
    lifesLabel.text = [NSString stringWithFormat:@"%i",lifes]; 
} 

- (IBAction)press1:(UIButton *)button1{ 
    if ((button1.selected=YES && click==0)){ 
     click=1; 
    } 
    else{ 
     lifes=lifes-1; 
     click=0; 
[self updateLifes:(lifes)]; 
} 
} 

- (IBAction)press2:(UIButton *)button2{ 
    if ((button2.selected =YES) && click==1){ 
     click=2; 
    } 
    else{ 
     lifes=lifes-1; 
     click=0; 
     [self updateLifes:(lifes)];  
    } 
} 

- (void)viewDidLoad{ 
    [super viewDidload]; 
    [self showLifes]; 
} 
+0

不理解您的問題! – Bruno

+0

如果用戶達到0生命,然後想再次玩,生命= 0而不是3 – fabersky

+1

euh ...你需要重置你的生命計數器。 – Bruno

回答

0

創建另一個按鈕「再玩一次」並將其鏈接到-(IBAction)buttonPressed:(UIButton *)playAgain並且在功能重置價值的生命值爲3.你不需要通過lifes,因爲它是一個全局變量(雖然這不是一個很好的做法)。雖然我不確定您的邏輯/ click的執行情況,但您可以使用以下(稍微優化的)代碼:

@implementation livello1 

    int click=0; 
    int lifes=3; 

-(void)updateLifes{ 
    if (lifes!=0) { 
     NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
     [standardDefaults setInteger:lifes forKey:@"UserLifes"]; 
     [standardDefaults synchronize]; 
     [self showLifes]; 
     livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil]; 
     livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self presentViewController:livello1view animated:YES completion:nil]; 
    } 
    else { 
     NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
     lifes=3; 
     [self showLifes]; 
     [standardDefaults setObject:@"3" forKey:@"UserLifes"]; 
     [standardDefaults synchronize]; 
     gioca *giocaview = [[gioca alloc] initWithNibName:nil bundle:nil]; 
     giocaview.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 
     [self presentViewController:giocaview animated:YES completion:nil]; 
    } 
} 

-(void)showLifes{ 
    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults]; 
    lifesLabel.text = [NSString stringWithFormat:@"%i",[standardDefaults integerForKey:@"Userlifes"]]; 
} 

- (IBAction)press1:(UIButton *)button1{ 
    if ((button1.selected=YES && click==0)){ 
     click=1; 
    } 
    else{ 
     lifes=lifes-1; 
     click=0; 
     [self updateLifes]; 
    } 
} 

- (IBAction)press2:(UIButton *)button2{ 
    if ((button2.selected =YES) && click==1){ 
     click=2; 
    } 
    else{ 
     lifes=lifes-1; 
     click=0; 
     [self updateLifes];  
    } 
} 

- (IBAction)buttonPressed:(UIButton *)playAgain{ //the new function 
    lifes = 3; 
    click = 0; 
    [self updateLifes]; 
} 

- (void)viewDidLoad{ 
    [super viewDidload]; 
    [self showLifes]; 
} 
+0

我使用單擊來檢測用戶是否按正確順序單擊按鈕。 順便說一句,非常感謝你的幫助! – fabersky

+0

沒有問題!希望上面的代碼有助於:) –

+0

現在我有一個不同的問題:當我第一次玩,一切順利。 當我再次玩,生命是3,但如果我點擊button2而不是按鈕1,視圖返回第一個,就像我有0的生命。 (對不起,我的英語) – fabersky

0

上playAgain按鈕點擊,setLife櫃檯3.