我有一切工作正常,也運行的定時器。但是當我遇到奇怪的問題時,我不得不重構我的cocos2d場景。 現在我無法再觸發NSTimers了,下面的BonusTimetimer甚至不會被解僱一次。NSTimer沒有開火
在此先感謝,下面是該代碼。
BonusTimeTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(CountTimeBonus:) userInfo:nil repeats:YES];
// (lately i added the line below, but it does not help)
[[NSRunLoop mainRunLoop] addTimer:BonusTimeTimer forMode:NSDefaultRunLoopMode];
-(void)CountTimeBonus:(NSTimer *) sender {
NSLog (@"Method -> Countimebonus");
if ((scoreTotal + 37) < TargetScore){
scoreTotal = scoreTotal + 37;
TimeBonus = TimeBonus-37;
NSString *level_timebonus = [NSString stringWithFormat:@"%d", TimeBonus];
[labelTimeBonus setString: level_timebonus];
NSString *scorestr = [NSString stringWithFormat:@"%d", scoreTotal];
[labelMainScore setString: scorestr];
[[SimpleAudioEngine sharedEngine] playEffect:@"light_switch_.mp3"];
}
else {
// add the Last few points and finish BonusTimer
scoreTotal = scoreTotal + TimeBonus;
TimeBonus=0;
NSString *level_timebonus = [NSString stringWithFormat:@"%d", timebonusgrayed];
[labelTimeBonus setString: @"" ];
[labelTimeBonusGrayed setString: level_timebonus];
NSString *scorestr = [NSString stringWithFormat:@"%d", scoreTotal];
[labelMainScore setString: scorestr];
[[SimpleAudioEngine sharedEngine] playEffect:@"light_switch_.mp3"] ;
[BonusTimeTimer invalidate];
BonusTimeTimer = nil;
timeBonusisdone = true;
timeBonusisactive = false;
}
}
的cocos2d的計時器也行,就好像遊戲暫停你所期望的,例如(即他們暫停射擊)的。使用cocos2d的定時器是很好的建議。 –
謝謝你的工作,對我來說。 這裏有一些consdider的東西, 1)代碼: [self schedule:@selector(CountTimeBonus :) delay:.01]; hase語法問題,使用此代替(下): [self schedule:@selector(CountTimeBonus :) interval:.01]; 2) –
thks,我更新了答案,以便其他人不必弄清楚:) – YvesLeBorg