Betkowski
Betkowski

Reputation: 491

Swift var not changing

First post, did try to search for an answer before writing, but none of the posts gave me the answer, so I'm asking. I bet it's something simple, that I just can't see.

I have a var for score. I want to add 1 to it every time collision takes place. Here's how it's written

var score:Int = 0
override func didMoveToView(view: SKView) {
var scoreOnScreen = SKLabelNode(text:"(\score)")

then, in the collide function, after bullet and the enemy are removed

func bulletDidCollideWithEnemy(bullet: SKSpriteNode, enemyOne: SKSpriteNode) {
  score++
  bullet.removeFromParent() 
  enemyOne.removeFromParent() 
}

But on the screen, it's still 0.

Why?

Upvotes: 0

Views: 58

Answers (1)

codester
codester

Reputation: 37189

You are not updating your score on UI.You are just udating variable score.Write code to update scoreOnScreen in bulletDidCollideWithEnemy

func bulletDidCollideWithEnemy(bullet: SKSpriteNode, enemyOne: SKSpriteNode) {
  score++
  scoreOnScreen.text = "\(score)"
  bullet.removeFromParent() 
  enemyOne.removeFromParent() 
}

Upvotes: 1

Related Questions