我想在我的比赛中得分,每秒需要加3分 我写了这样的代码
var scoreLabel = SKLabelNode()
let scoreLabelName = "scoreLabel"
var count = 0
override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
physicsWorld.contactDelegate = self
addPlayer()
scoreLabel = SKLabelNode(fontNamed: "ScoreLabel")
scoreLabel.name = scoreLabelName
scoreLabel.fontSize = 125
scoreLabel.fontColor = SKColor.white
scoreLabel.text = "\(count)"
print(size.height)
scoreLabel.position = CGPoint(x: frame.size.width / 2, y: frame.size.height - 300)
self.addChild(scoreLabel)
}
答案 0 :(得分:1)
请记住......在SpriteKit中,您可以定期调用update
方法,并为您提供currentTime
:)
所以你可以:
currentTime
update
进行检查
currentTime
匹配的值,并更新您想要更新的内容。所以在你的情况下,有些东西:
var scoreLabel = SKLabelNode()
let scoreLabelName = "scoreLabel"
var count = 0
var lastUpdateTime: TimeInterval?
然后更新:
override func update(_ currentTime: TimeInterval) {
var delta = TimeInterval()
if let last = lastUpdateTime {
delta = currentTime - last
} else {
delta = currentTime
}
if delta > 1.0 {
//tick tock, a second has passed, update lastUpdateTime
lastUpdateTime = currentTime
count += 3
scoreLabel.text = "\(count)"
}
}}
希望能给你一些线索。