swift和spritekit中的倒计时

时间:2014-07-21 12:44:20

标签: xcode swift countdown ios8

我正在尝试将countDown放在我的应用程序上。我正在使用SKLabelNode。这是我的代码:

class PlayScene: SKScene {
    var startCount = true
    var setTime = 0
    var myTime = 0
    var printTime = SKLabelNode(fontNamed: "arial")
    override func didMoveToView(view: SKView!) {
        self.backgroundColor = UIColor(hex: 0xD64541)
        self.printTime.text = NSString(format: "%i", self.myTime)
        self.addChild(self.printTime)
    }
    override func update(currentTime: NSTimeInterval) {
        if self.startCount == true {
            self.setTime = Int(currentTime)
            self.startCount = false
        }
        self.myTime = Int(currentTime) - self.setTime
        }
  }

编译它没有问题,但它在执行时崩溃了...... 我认为这是来自我在字符串中的转换..

谢谢

3 个答案:

答案 0 :(得分:2)

我希望这会有所帮助。你几乎拥有它

class PlayScene: SKScene {

var startCount = true
var setTime = 0
var myTime = 0
var printTime = SKLabelNode(fontNamed: "arial")

override func didMoveToView(view: SKView) {
    self.backgroundColor = UIColor.orangeColor()

    //self.printTime.text = NSString(format: "%i", self.myTime)//you need the position
    self.printTime.position = CGPointMake(self.size.width * 0.5, self.size.height * 0.5)
    self.addChild(self.printTime)
}
override func update(currentTime: NSTimeInterval) {
    if self.startCount == true {
        self.setTime = Int(currentTime)
        self.startCount = false
    }
    self.myTime = Int(currentTime) - self.setTime
     self.printTime.text = ("format:\(self.myTime)")//put your label in update so it shows each second.
}

答案 1 :(得分:2)

以下是我为Swift / SpriteKit'Breakout'应用程序解决此问题的方法。我希望在主游戏屏幕上从5比1倒数,但在球开始移动之前。我添加了这些功能,然后在*.sh text eol=lf 结束时调用倒计时(5):注意didMoveToViewball.physicsBody!.applyImpulse(CGVectorMake(20, 20))的最后一步,它启动球并有效地开始游戏。

endCountdown

答案 2 :(得分:0)

float  timeforFunction;
   int numberoftime;
   int __totalscore;
   timeforFunction=1.5f;
   numberoftime=10;

    SKAction *updateTime= [SKAction sequence:@[
                                               [SKAction waitForDuration: timeforFunction],
                                               [SKAction performSelector:@selector(updatescore)
                                                                onTarget:self]

                                               ]];
    [self runAction:[SKAction repeatAction:updateTime count: numberoftime] withKey:@"zorbTime"];


   -(void) updatescore
{
    __totalscore+=1;
   _GamescoreText.text = [NSString stringWithFormat:@“%i”, __totalscore];
}

如果你想永远重复这个动作,只需改为

[self runAction:[SKAction repeatAction:updateTime count: numberoftime] withKey:@"zorbTime"];