Swift 在SpriteKit中设置数字更改动画

Swift 在SpriteKit中设置数字更改动画,swift,sprite-kit,sklabelnode,Swift,Sprite Kit,Sklabelnode,我正在使用Swift和SpriteKit。 例如,我想要一个数字从20变为10。我想在它变化时显示数字的变化。更具体地说,我希望19显示0.1秒,18显示0.1秒,依此类推。我找不到任何方法来做这件事。需要补充的一点是:数字20和数字10在任何时候都可能不同,因此代码不应该只适用于那些特定的数字,但是,我发现代码可以更改文本: var counterNumber = 20 var label = SKSpriteLabel() //... label.text = "\(counterNumbe

我正在使用
Swift
SpriteKit
。 例如,我想要一个数字从20变为10。我想在它变化时显示数字的变化。更具体地说,我希望19显示0.1秒,18显示0.1秒,依此类推。我找不到任何方法来做这件事。需要补充的一点是:数字20和数字10在任何时候都可能不同,因此代码不应该只适用于那些特定的数字,但是,我发现代码可以更改文本:

var counterNumber = 20
var label = SKSpriteLabel()
//...
label.text = "\(counterNumber)"
label.fontName = "Helvetica Neue"
label.fontSize = 30
label.fontColor = SKColor.blackColor()
//...
counterNumber = 10
label.text = "\(counterNumber)"

先谢谢你。。。Anton

您可以在
SKScene
中使用
update
功能。它有一个参数-当前时间。使用它(以及在加载场景时设置的开始时间),可以知道场景中经过了多少时间。然后,正如您已经展示的那样,只需更新标签即可。

您可以在
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))


}
var计数器编号=20
var counterLabel=SKLabelNode()
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
counterLabel.text=“\(计数器编号)”
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(计数器标签)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
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(序列,计数:10))
}
我找到了一个答案:

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))


}
var计数器编号=20
var counterLabel=SKLabelNode()
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
counterLabel.text=“\(计数器编号)”
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(计数器标签)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
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(序列,计数:10))
}