2017-01-09 54 views
0

我很难让游戏得分按时间而不是游戏事件增加。得分是游戏开始时显示的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) 

回答

0

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

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

没有运气,仍然显示0 – TroyDeJ

+0

@TroyDeJ - 你真的调用过定时器吗?尝试在ScoreAndHighScore方法中设置一个断点,以确保它甚至被调用。如果不是,那么尝试''viewDidLoad''中的'''timer.scheduledTimer'(timeInterval:5.0,target:self,selector:#selector(ScoreAndHighScore),userInfo:nil,repeatats:true)''''''''只需要测试 – Pierce

+0

既尝试过,也没有成功,让我觉得代码是完全错误的。 – TroyDeJ

0

我会推荐使用SKActions作为你的计时器而不是NSTimer。你可以这样做:

let wait = SKAction.wait(forDuration: 1.0) 
let incrementScoreAction = SKAction.run { 
    //code to increment score 
} 

let repeatIncrementScore = SKAction.repeatForever(SKAction.sequence([wait, incrementScoreAction])) 

self.run(repeatIncrementScore) 
+0

我会如何做与skactions相同的事情? – TroyDeJ

+0

@TroyDej,看我更新的答案。 – claassenApps

+0

刚刚尝试过,效果很好,而且是一种更好的方法。非常感谢你 – TroyDeJ

相关问题