2012-01-20 41 views
0

我有一個tap計數器,這是「Who Wins」代碼。每當Flipped(int timer)等於30時,該應用程序都會崩潰,並且它正在決定誰的抽頭數最多。它總是說「玩家2贏」並凍結。請幫忙。 Number是一個攻絲者的數值,Number1是第二個攻絲者的數值。我該如何解決?Objective-C代碼掛起

- (void)countup 
{ 
    if (Fliped == 30)  
    { 
     //error message 
     if (Number < Number1) 
     { 
      myAlertView = [[UIAlertView alloc] initWithTitle:@"Stop!" message:@"Player 2 Wins!" delegate:self 
              cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      timer = [NSTimer scheduledTimerWithTimeInterval:0.0 
                target:self selector:@selector(countup)userInfo:nil repeats:YES]; 

      [myAlertView show]; 
     } 
     if (Number > Number1) 
     { 

      myAlertView = [[UIAlertView alloc] initWithTitle:@"Stop!" message:@"Player 1 Wins!" delegate:self 
              cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
      timer = [NSTimer scheduledTimerWithTimeInterval:0.0 
                target:self selector:@selector(countup)userInfo:nil repeats:YES]; 

      [myAlertView show]; 

     } 
    else 
    { 
     MainInt += 1; 
     seconds.text = [NSString stringWithFormat:@"%d", MainInt]; 
     Fliped += 1; 
     secondsFlip.text = [NSString stringWithFormat:@"%d", Fliped]; 
    } 
} 

在此先感謝。

什麼可能是這次崩潰的原因?

+1

不客氣! (你有問題嗎?) –

+0

我不知道,但你可能需要@selector(計數進位)和USERINFO – rubixibuc

+0

之間的空間這有沒有關係Xcode和我已經刪除從標題自己的標籤和文字如果在我可以保存自己的編輯之前它沒有被編輯幾次;-) –

回答

3

當變量Fliped爲30時,您正在遞歸調用自己的方法countup無限次數。這就是爲什麼你會崩潰。不要這樣做。

+0

謝謝你的工作,我創建了一個 - (void)stop來停止整數,並將@selecter改成(stop) – ch1pa

+0

@ ch1pa這完全是錯誤的事情。你還創建了一個定時器,它會一直呼叫'stop'。你爲什麼要在那裏放一個計時器?如果你想停止一個定時器,使用'invalidate'就可以了。如果您想在檢測到贏家時致電'停止',請致電'[self stop]' –

+0

謝謝Tim Kemp,這比其他解決方案的效果更好。 – ch1pa