2015-06-24 173 views
0

故事板設置在2個VC上。 initial VC只是一個按鈕,其中seguesbattleScene發生。該battleScene有一個按鈕,其中segues回到initial VC,顯示健康的標籤,以及一個啓動計時器的按鈕。計時器在想要停止時不停止,不想停止時Obj-C

我有兩個問題,我一直在努力解決,並沒有想通。

1:當我玩NSTimer然後切換VC的,回到了定時器是影響標籤停止更新標籤的VC。

2:當enemy2的健康是0,我告訴計時器停止使用我的方法stopTimer定時器繼續。

int enemy001Hp = 100; 
int enemy002Hp = 200; 

bool enemy001Active = NO; 
bool enemy002Active = NO; 

- (void) enemy1 { 
enemy001Active = YES; 
self.enemyHpLabel.text = [NSString stringWithFormat:@"%i", enemy001Hp]; 
} 
- (void) enemy2 { 
enemy002Active = YES; 
self.enemyHpLabel.text = [NSString stringWithFormat:@"%i", enemy002Hp]; 
} 

- (void) enemyDamageTimer { 
if (enemy001Active == YES) { 
    enemy001Hp -= 50; 
    self.enemyHpLabel.text = [NSString stringWithFormat:@"%i",enemy001Hp]; 
    if (enemy001Hp <= 0) { 
     [self enemy2]; 
     self.enemyHpLabel.text = [NSString stringWithFormat:@"%i", enemy002Hp]; 
     enemy001Active = NO; 
     enemy002Active = YES; 
    } 
} else if (enemy002Active == YES) { 
    enemy002Hp -=50; 
    self.enemyHpLabel.text = [NSString stringWithFormat:@"%i", enemy002Hp]; 
} 
} 

- (void) stopTimer { 
[timer invalidate]; 
timer = nil; 
} 

- (void)viewDidLoad { 
[super viewDidLoad]; 
[self enemy1]; 
if (enemy002Active == YES) { 
    self.enemyHpLabel.text = [NSString stringWithFormat:@"%i", enemy002Hp]; 
} 
} 

- (IBAction)idleAttack:(id)sender { 
self.idleOutler.hidden = YES; 
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(enemyDamageTimer) userInfo:nil repeats:YES]; 
if (enemy001Active == YES) { 
    if (enemy001Hp <= 0) { 
     [self enemy2]; 
     enemy001Active = NO; 
     enemy002Active = YES; 
    } 
} else if (enemy002Active == YES) { 
    if (enemy002Hp <= 0) { 
     self.enemyHpLabel.text = [NSString stringWithFormat:@"0"]; 
     enemy002Hp = 0; 
     enemy002Active = NO; 
     [self stopTimer]; 
    } 
} 
} 

我從一個計時器引導stopTimer方法,我一直在尋找四處打聽如何解決的問題賽格瑞計時器停止沒有決心。

乾杯

回答

0

對於問題1有沒有錯在你對定時器的代碼,所以這個問題可能仍然在VC過渡代碼

問題2定時器將反覆火法enemyDamageTimer中,你永遠不會稱爲stopTimer。這就是爲什麼定時器繼續

+0

所以只要我把'enemyDamageTimer'它實際上將停止內部的'stopTimer'方法?此外,對於第一季度,我沒有VC轉換的代碼,我可以做些什麼來使其工作? – Memj