使计时器倒计时并在Cocos2D屏幕上显示得分

时间:2009-11-26 04:42:26

标签: iphone objective-c cocos2d-iphone

如何在Cocos2D和分数计数器中应用计时器倒计时,两者都将显示在屏幕上?

我还是新手

请帮帮我

非常感谢

1 个答案:

答案 0 :(得分:2)

您应该查看Timer课程。这是一个link to the Timer class overview - cocos2d for iPhone 0.8.2。你不需要直接(但是间接地)实现一个,但知道它是如何工作的很好。

要使用cocos2d在屏幕上显示实际计数器,请查看LabelAtlas。这是link to LabelAtlas class overview - cocos2d for iPhone 0.8.2

/* basically you create a LabelAtlas subclass that
   has a method for updating itself with the current time
   or score. I will call it ScoreLabel in this example */
ScoreLabel * bonusScore = [[ScoreLabel alloc] initWithString:@"0"
                                              charMapFile:@"charMap.png"
                                              itemWidth:10
                                              itemHeight:10
                                              startCharMap:'0'];
// then schedule a method to be called that updates the current score
// schedule: paramter of type SEL. It should pass the method to be scheduled
// interval is in seconds, it determines how often the method is called.
[bonusScore schedule:@selector(updateScore) interval:0.5];

/* the implementation of the updateScore method could be */
-(void)updateScore {
    [self setString: [[World sharedWorld] timeLeft]]];
}

查看AtlasSprites(以及LabelAtlas)的cocos2d示例,了解如何实现支持该类所需的图像。