0

我的一切工作正常,计时器也在运行。但是当我遇到奇怪的问题时,我不得不重新构建我的 cocos2d 场景。现在我不能再触发 NSTimer 了,下面的 BonusTimer 甚至没有被触发一次。

先谢谢了,下面是代码。

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;
    }

}

4

2 回答 2

3

我从来没有真正深入研究过那个兔子洞(即为什么会这样),但是这里一直有传言说 NSTimer 和 cocos2d 不能很好地混合。相反,我使用 cocos 自己的方法

[self schedule:@selector(CountTimeBonus:) interval:.01];

// and to invalidate this


[self unschedule:@selector(CountTimeBonus:)];

CountTimeBonus 签名将是:

-(void) CountTimeBonus:(ccTime) dt {
}
于 2013-06-23T12:08:01.227 回答
0

感谢为我工作的 YvesLeBorg。

还有几件事要考虑,

1) cocos2d 代码:

[self schedule:@selector(myTimer:) delay:.01];

有语法问题,我用这个代替(下):

[self schedule:@selector(myTimer:) interval:.01];

2)我让 NSTimer 再次工作,但是当我将 NSTimer 调用放在 do-while 循环中时,它不会工作。

再次感谢,祝你好运

约翰

于 2013-06-24T10:47:19.150 回答