SpriteKit-创建一个计时器

SpriteKit-创建一个计时器

如何创建一个计时器,它每隔两秒钟就会在我的屏幕上的HUD上增加一个分数?这是我对HUD的代码:

    @implementation MyScene{
    int counter;
    BOOL updateLabel;
    SKLabelNode *counterLabel;}-(id)initWithSize:(CGSize)size{
    if (self = [super initWithSize:size])
    {
        counter = 0;

        updateLabel = false;

        counterLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
        counterLabel.name = @"myCounterLabel";
        counterLabel.text = @"0";
        counterLabel.fontSize = 20;
        counterLabel.fontColor = [SKColor yellowColor];
        counterLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
        counterLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeBottom;
        counterLabel.position = CGPointMake(50,50); // change x,y to location you want
        counterLabel.zPosition = 900;
        [self addChild: counterLabel];
    }}


白猪掌柜的
浏览 780回答 3
3回答

萧十郎

在SWIFT中可用:var timescore = Int()  var actionwait = SKAction.waitForDuration(0.5)             var timesecond = Int()             var actionrun = SKAction.runBlock({                     timescore++                     timesecond++                     if timesecond == 60 {timesecond = 0}                     scoreLabel.text = "Score Time: \(timescore/60):\(timesecond)"                 })             scoreLabel.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))

森林海

我已经做了上面的快速例子,并为时钟增加了前导零点。&nbsp;&nbsp;&nbsp;&nbsp;func&nbsp;updateClock()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;leadingZero&nbsp;=&nbsp;"" &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;leadingZeroMin&nbsp;=&nbsp;"" &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;timeMin&nbsp;=&nbsp;Int() &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;actionwait&nbsp;=&nbsp;SKAction.waitForDuration(1.0) &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;timesecond&nbsp;=&nbsp;Int() &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;actionrun&nbsp;=&nbsp;SKAction.runBlock({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timeMin++ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;timesecond++ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;timesecond&nbsp;==&nbsp;60&nbsp;{timesecond&nbsp;=&nbsp;0} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;timeMin&nbsp;&nbsp;/&nbsp;60&nbsp;<=&nbsp;9&nbsp;{&nbsp;leadingZeroMin&nbsp;=&nbsp;"0"&nbsp;}&nbsp;else&nbsp;{&nbsp;leadingZeroMin&nbsp;=&nbsp;""&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;timesecond&nbsp;<=&nbsp;9&nbsp;{&nbsp;leadingZero&nbsp;=&nbsp;"0"&nbsp;}&nbsp;else&nbsp;{&nbsp;leadingZero&nbsp;=&nbsp;""&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.flyTimeText.text&nbsp;=&nbsp;"Flight&nbsp;Time&nbsp;[&nbsp;\(leadingZeroMin)\(timeMin/60)&nbsp;:&nbsp;\(leadingZero)\(timesecond)&nbsp;]" &nbsp;&nbsp;&nbsp;&nbsp;}) &nbsp;&nbsp;&nbsp;&nbsp;self.flyTimeText.runAction(SKAction.repeatActionForever(SKAction.sequence([actionwait,actionrun])))}
打开App,查看更多内容
随时随地看视频慕课网APP