我正在使用Swift
和SpriteKit
。我想要一个数字从 20 更改为 10。我希望在它的变化时显示数字的变化。更具体地说,我希望 19 显示 0.1 秒,18 显示 0.1 秒,依此类推。
您可以在SKScene
中使用update
函数。它有一个参数 - 当前时间。使用它(也许是场景加载时设置的开始时间(,您可以知道场景中经过了多少时间。然后,只需更新标签即可,如您所见。
我找到了一个答案:
var counterNumber = 20
var counterLabel = SKLabelNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
counterLabel.text = "(counterNumber)"
counterLabel.fontName = "Helvetica Neue"
counterLabel.fontSize = 100
counterLabel.fontColor = SKColor.blackColor()
counterLabel.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
self.addChild(counterLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
let wait = SKAction.waitForDuration(0.5)
let block = SKAction.runBlock({
self.counterNumber = self.counterNumber - 1
self.counterLabel.text = "(self.counterNumber)"
})
let sequence = SKAction.sequence([wait, block])
counterLabel.runAction(SKAction.repeatAction(sequence, count: 10))
}