我的一切工作正常,也就是跑步的计时器。但当我遇到奇怪的问题时,我不得不重组我的cocos2d场景。 现在我再也无法解雇NSTimers,下面的BonusTimetimer甚至没有被解雇过一次。
先谢谢,下面是代码。
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;
}
}
答案 0 :(得分:3)
我从来没有真正钻过那个兔子洞(也就是为什么这样),但是这里有一个持续的谣言,NSTimer和cocos2d混合不好。相反,我使用cocos自己的方法
[self schedule:@selector(CountTimeBonus:) interval:.01];
// and to invalidate this
[self unschedule:@selector(CountTimeBonus:)];
CountTimeBonus签名将是:
-(void) CountTimeBonus:(ccTime) dt {
}
答案 1 :(得分:0)
感谢YvesLeBorg为我工作。
还有一些事情需要考虑,
1)cocos2d代码:
[self schedule:@selector(myTimer:) delay:.01];
有语法问题,我用它来代替(下面):
[self schedule:@selector(myTimer:) interval:.01];
2)我让NSTimer再次工作,但是当我将NStimer调用放入do-while循环时,它将无法工作。
再次感谢,祝你好运
约翰