swift - 当按下开始按钮时,游戏分数会随着时间的推移而增加

标签 swift timer sprite-kit

我很难让游戏分数随着时间而不是游戏事件的增加而增加。分数是游戏开始时显示的SKlabelnode。我希望分数随着游戏时间的推移而增加,然后在游戏重新开始时重新回到0(即:玩家死亡)

分数标签保持在 0,我尝试了多种代码变体

//score
   var score = Int()
   var highScore = Int()
   let scoreLable = SKLabelNode()





    scoreLable.position = CGPoint(x: self.frame.width / 1.1, y: self.frame.height / 1.1)
    scoreLable.zPosition = 5
    scoreLable.fontSize = 100
    scoreLable.fontName = "AppleSDGothicNeo-bold"
    scoreLable.text = "\(score)"




func ScoreAndHighScore(){
   score = score+1
   }

var scoreTimer = Timer(timeInterval: 5.0, target: self, selector: #selector(ScoreAndHighScore), userInfo: nil, repeats: true)

最佳答案

每次分数值增加时,您都需要更新标签的文本。

func ScoreAndHighScore() {
    score += 1
    scoreLabel.text = "\(score)"
}

关于swift - 当按下开始按钮时,游戏分数会随着时间的推移而增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41540182/

相关文章:

ios - 如何在我的 SKLabelNode 中显示我的计时器?

如果 API 中存在空白或 null,则 JSON 序列化

C# 定时器回调

ios - Swift 3,展开外设名称(蓝牙 BLE)

javascript - 在javascript中刷新页面后继续时钟时间

.net - 从 C# 中的计时器获取准确的滴答声

ios7 - Sprite Kit 的关卡编辑器?

ios - 通过触摸在枚举状态之间切换

ios - UIWebView 不显示我的网页

ios - POST 请求响应后启动 segue