1

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?

gkolman
  • 189
  • 10

2 Answers2

0

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...

MirekE
  • 11,515
  • 5
  • 35
  • 28
  • I called it in my didMoveToView function. How do I update the text? do i use the update method? because i also have a print method in there for my debugger and that is not updating either it just says 1 every time instead of adding 1 every time @MirekE – gkolman Jun 02 '16 at 18:10
  • Yes, put it to the update function. Not sure why your print does not reflect the addition. Put another print before the addition to check that you are not assigning 0 somewhere... – MirekE Jun 02 '16 at 18:34
  • i assigned 0 as the base value of my integer i think thats what keeps happening is my integer keeps defaulting to 0. but why wouldn't it change? @MirekE – gkolman Jun 02 '16 at 20:53
  • Setting it to 0 in the beginning is fine. The important part is if you are *resetting* it back to 0... – MirekE Jun 02 '16 at 22:18
  • I don't think i am, I've given all of the code, do you not know why it is not updating? @MirekE – gkolman Jun 02 '16 at 23:48
  • No, I don't except perhaps that you create the GameScene after every kill or otherwise reset the counter to 0 in a code that is not included. Try the second print that I suggested before. – MirekE Jun 03 '16 at 01:10
0

Update your label text after updating your Enemy1KillCounter variable.

func updateEnemy1KillCounter() {
    Enemy1KillCounter = Enemy1KillCounter + 1
    Enemy1KillCounterLabel.text = "\(Enemy1KillCounter)"
    print(Enemy1KillCounter)
}
Muzahid
  • 5,072
  • 2
  • 24
  • 42
  • I tried this method, however the label does print anything at all when i declare its Enemy1KillCounterLabel.text ="\(Enemy1KillCounter)" in any other method besides the createEnemyKilledLabel() @Md.MuzahidulIslam – gkolman Jun 02 '16 at 17:49
  • You are missing the leading backslash in your comment, maybe that is it? But to be sure you could use a temporary string to hold the recast value of Enemy1KillCounter and assign that. `let killCountString = Enemy1KillCounter.description; Enemy1KillCounterLabel.text = killCountString` – rebusB Jun 06 '16 at 16:58