Search code examples
swiftsprite-kitnodessklabelnode

Integer value not updating


I am creating a game and I am trying to keep a record of all enemy's killed but my SKLabel node is not updating. Here's how I'm implementing it

class GameScene: SKScene, SKPhysicsContactDelegate {

var Enemy1KillCounter:Int  = 0
var Enemy1KillCounterLabel = SKLabelNode ()

 override func didMoveToView(view: SKView) {

     createEnemyKilledLabel()
}

    func createEnemyKilledLabel() {

    Enemy1KillCounterLabel.text = "\(Enemy1KillCounter)"
    Enemy1KillCounterLabel.fontSize = 65
    Enemy1KillCounterLabel.fontColor = SKColor .blackColor()
    Enemy1KillCounterLabel.position = CGPointMake(400, 400)

    self.addChild(Enemy1KillCounterLabel)
}

 func updateEnemy1KillCounter() {

    Enemy1KillCounter = Enemy1KillCounter + 1

    print(Enemy1KillCounter)

}
// I use the next method because i call this method in my enemy class    
   when the enemy is "killed"

  func Enemy1DieG () {
    updateEnemy1KillCounter()
}

Does anybody know why my label is not being updated?


Solution

  • When you update Enemy1KillCounter, you also need to update the Enemy1KillCounterLabel.text with the new value. Besides, I don't see where your createEnemyKilledLabel() is called. Make sure it is called somewhere.

    A side note - variable names typically start with lowercase, like enemy1KillCounterLabel. Following the standards makes the code easier to read by others...