首页 文章

我需要在我的SpriteKit游戏中实现一个计时器?

提问于
浏览
0

在我的游戏中,我需要实现一个30秒计时器,当我点击屏幕时启动,这样如果玩家在计时器达到0之前没有达到目标,则游戏结束 .

我无法弄清楚如何在触摸屏幕后将计时器数量降至零,它只保持30秒 . 我尝试实现一个NStimer,但我知道你必须添加一个我不想要的PAUSE功能,加上NStimer在我触摸屏幕之前倒数,这也是我不想要的 .

到目前为止这是我的代码:

var TimerNode: Int = 30
    var  TimerLabel = SKLabelNode(fontNamed: "STHeitJ-Medium")

    TimerLabel.text = "\(TimerNode)"
    TimerLabel.fontSize = 40
    TimerLabel.position.x = size.width / 2
    TimerLabel.position.y = size.height / 8.5
    TimerLabel.zPosition = 3.00
    TimerLabel.fontColor = UIColor.whiteColor()
    addChild(TimerLabel)

2 回答

  • 0
    self.addChild(myLabel)
    

    您可以实现这样的计时器:

    //Timer that updates Label:
    
    myLabel.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
                TimerNode -= 1
                TimerLabel.text = "\(TimerNode)"
    
                if TimerNode <= 0 {
    
                     //Game Over code
                }
                },SKAction.wait(forDuration: 1)])))
    

    旧:

    //Run when clicked: 
    var wait = SKAction.waitForDuration(30)
    var run = SKAction.runBlock {
        //Game Over
    }
    self.runAction(SKAction.sequence([wait, run]))
    
  • 0
    // ----- TIMER 2 Sec. ------
    removeAction(forKey: "Timer") // Timer delete
    var time = 20                 // start counter
    
    self.run(SKAction.repeat(SKAction.sequence([SKAction.run {
        time -= 1
        print("\(time)")
    
         if time <= 0 {
             // start your code here!
         }
    
    },SKAction.wait(forDuration: 0.1)]), count: time), withKey: "Timer")
    

相关问题