简体   繁体   English

Swift变量给出错误的值

[英]Swift variable giving wrong value

I am making a game in swift, I have a variable called score , I increment and print this variable in my onTick() method, and it works fine. 我正在快速制作游戏,我有一个名为score的变量,我将其递增并在onTick()方法中打印此变量,并且效果很好。 However when I try to access this variable to display the final score after the player dies, I get 0 or whatever the initial value was. 但是,当我尝试访问此变量以显示玩家死亡后的最终分数时,我得到0或任何初始值。

Initialization: 初始化:

class ViewController: UIViewController {
var score = 0;

Incrementing: 递增:

scoreCounter.text = String(self.score);
self.score += 1;

Trying to display it at endgame: 尝试在残局显示它:

 print("SCORE: \(score)");
if (score > topScore) {
    endScore.textColor = UIColor.greenColor();
    endScore.text = "NEW HIGHSCORE! Score: " + String(score) + "   HighScore: " + String(topScore);
} else {
    endScore.text = "Score: " + String(score) + "   HighScore: " + String(topScore);
}

Any insight is appreciated! 任何见识表示赞赏! :) :)

我通过使变量为静态来解决此问题,我不知道这是否是正确的解决方案,但它对我有用,感谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM