1

crash screen/ error message Im creating a game and i have created and integer which keeps track of the number of enemies killed, however i can not get the sklabel on the UI to update. no matter how many enemies the integer kept displaying 0. Here are some of the methods i have tried

class GameScene: SKScene, SKPhysicsContactDelegate {

var Enemy1KillCounter = 0
var Enemy1KillCounterLabel = SKLabelNode ()

override func didMoveToView(view: SKView) {

    createEnemyKilledLabel()
}

func createEnemyKilledLabel() {

    Enemy1KillCounterLabel.fontSize = 65
    Enemy1KillCounterLabel.fontColor = SKColor .blackColor()
    Enemy1KillCounterLabel.position = CGPointMake(400, 400)
    self.addChild(Enemy1KillCounterLabel)
}

func updateEnemy1KillCounter() {

    Enemy1KillCounter += 1
    Enemy1KillCounterLabel.text = "\(Enemy1KillCounter)"
    score = score + 15
    //enemy1Killed = true   I had tried Boolean values as well 
    print("updateEnemy1KillCounter")

}
    //this method is called in my enemy1 class when its "killed"
func Enemy1DieG () {

    updateEnemy1KillCounter()

    }
}

i have also tried using the update method with boolean values implicated multiple different ways but none worked.

    override func update(currentTime: CFTimeInterval) {

    if enemy1Killed {Enemy1KillCounter += 1}
    Enemy1KillCounterLabel.text = "\(Enemy1KillCounter)"

 }

here is my enemy class and where i am calling the "enemy1DieG" method

I call the hit method in a contact method but i know that is not the issue because when the enemy is is "killed" when it is hit 5x's so the enemy1die and killCounter methods are being called

class Enemy1: SKNode {

var Enemy1Health:Int = 50

 func hit() ->Bool {

Enemy1Health -= 10
Bullet1GoAway = true

if ( Enemy1Health <= 0 ) {

        Enemy1Die()
    return true
    } else {
    return false
}
}
func Enemy1Die () {
    self.removeFromParent()
    Enemy1KillCounter()
}

func Enemy1KillCounter (){
    GameScene().Enemy1DieG()
}
}

its so weird all of the methods are being called i used print values on each method to make sure but my integer is not updating in my UI it just keeps displaying 0. This is probably such an easy answer and I keep missing it, if anybody can help that'd be awesome.

gkolman
  • 189
  • 10

1 Answers1

1

When inside Enemy1KillCounter() you write

GameScene().Enemy1DieG()

you are NOT using the GameScene shown on the screen.

Instead you are temporarily creating a new empty screen, invoking Enemy1DieG() on it and they destroying it.

Please replace this

func Enemy1KillCounter (){
    GameScene().Enemy1DieG()
}

with this

func Enemy1KillCounter() {
    guard let gameScene = self.scene as? GameScene
        else { fatalError("Current node is not inside a GameScene") }
    gameScene.Enemy1DieG()
}
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
  • for your 1st theory - are you only able to have 1 SKlabel per scene? or do you mean i have 2 of the same labels and I'm printing one and not the other for your 2nd - my background is grey so i can see that it is displaying 0 its just not updating and for your third- what do you men where does the label come from? i created the label in the game scene class and call it in the view method @appzYourLife – gkolman Jun 07 '16 at 20:25
  • @gkolman: you can have as many labels as you want. I suspect there is another label in your scene (the one visible via the simulator), this would explain why the changes to `Enemy1KillCounterLabel` aren't visible. I updated by answer with a test code. Please use it and let me know what you see in your label into the simulator. – Luca Angeletti Jun 07 '16 at 20:27
  • the text says "Do you see me?" lol how should i proceed? @appzYourLife – gkolman Jun 07 '16 at 22:08
  • yessir :D @appzYourLife and i changed my variables to lowercase – gkolman Jun 07 '16 at 22:13
  • @gkolman: Then I don't understand. Another question. When an enemy gets killed, how are you calling `enemy1DieG()`? Can you show me the full block of code? – Luca Angeletti Jun 07 '16 at 22:27
  • Just added my enemy class @appzYourLife thanks for the help – gkolman Jun 07 '16 at 23:06
  • i implemented your code, thank you for your help however, is there anything else i am missing? The screen integer is still not updating :( it still displays a 0 @appzYourLife – gkolman Jun 08 '16 at 05:56
  • Do you have any other ideas as to why it is not updating? @appzYourLife i have been stuck on this for like 2 days :( – gkolman Jun 08 '16 at 17:01
  • @gkolman: if you want you can send me your project: info@appzyourlife.com – Luca Angeletti Jun 08 '16 at 17:04
  • Ok i implemented your code and it crashes on this line(I added a screenshot of the crash), i feel like if we fix the crash it should work fine lol @appzYourLife – gkolman Jun 08 '16 at 19:34
  • @gkolman: good we are very close. It means that when you invoke `Enemy1KillerCounter()` your `Enemy has already been removed from the your scene. Can you change your code to call `Enemy1KillerCounter()` *before* the `Enemy` is removed? – Luca Angeletti Jun 08 '16 at 20:25
  • oh, i just actually read the whole error... lol i figure it out. thank you soooooo much you have finally stopped my headache haha. It would have taken me a couple more days to figure that out on my own. thanks for the help you're a rockstar @appzYourLife – gkolman Jun 08 '16 at 21:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114275/discussion-between-appzyourlife-and-gkolman). – Luca Angeletti Jun 09 '16 at 19:43