我想创造这种效果,分数随着每个数字之间的延迟而变化,就像 Flappy Bird 游戏结束屏幕上的分数一样。
在这个例子中,它应该在我触摸屏幕时开始计数。
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
// Global declaration of objects
var scoreLabel = SKLabelNode()
var score:Int = 15
override func didMoveToView(view: SKView) {
/* Setup your scene here */
scoreLabel = SKLabelNode(fontNamed: "RubberBiscuitBold")
scoreLabel.fontSize = 50
scoreLabel.fontColor = SKColor.blackColor()
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
scoreLabel.zPosition = 1000
self.addChild(scoreLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for var i = 0; i <= score; i++ {
scoreLabel.runAction(SKAction.waitForDuration(1))
scoreLabel.text = "\(i)"
print("\(i)")}
}
最佳答案
尝试这样的事情:
for var i = 0; i <= 10; i++ {
let seconds: Double = 0.5
let count = i
let delay = seconds * Double(NSEC_PER_SEC) // nanoseconds per seconds
let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(i)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
print("\(count)")
scoreLabel.text = "\(count)" })
}
关于swift - 延迟更改分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35129203/