0
故事板設置在2個VC上。 initial VC
只是一個按鈕,其中segues
到battleScene
發生。該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
方法,我一直在尋找四處打聽如何解決的問題賽格瑞計時器停止沒有決心。
乾杯
所以只要我把'enemyDamageTimer'它實際上將停止內部的'stopTimer'方法?此外,對於第一季度,我沒有VC轉換的代碼,我可以做些什麼來使其工作? – Memj