Search code examples
swiftswift3sklabelnode

Is it possible to update an SKLabelNode?


I'm trying to update the coinLabel, so that when the "life" node is tapped, it will update the SKLabelNode and display the correct coins after they have been subtracted from the total. I'm new to coding, so if there is anything else wrong with this please reply! Thanks!

import Foundation
import SpriteKit

class ShopScene: SKScene {

override func didMove(to view: SKView) {


    let background = SKSpriteNode(imageNamed: "background")
    background.size = self.size
    background.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
    background.zPosition = 0
    self.addChild(background)

    let mainMenu = SKLabelNode(fontNamed: "The Bold Font")
    mainMenu.text = "Main Menu"
    mainMenu.fontSize = 100
    mainMenu.fontColor = SKColor.darkGray
    mainMenu.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.3)
    mainMenu.zPosition = 1
    mainMenu.name = "Main Menu"
    self.addChild(mainMenu)

    let Life = SKLabelNode(fontNamed: "The Bold Font")
    Life.text = "Click here to buy 1 life!"
    Life.fontSize = 130
    Life.fontColor = SKColor.darkGray
    Life.position = CGPoint(x: self.size.width*0.5, y: self.size.height*0.6)
    Life.zPosition = 1
    Life.name = "Life"
    self.addChild(Life)

    let coinLabel = SKLabelNode(fontNamed: "The Bold Font")
    coinLabel.text = "Coins: \(coinNumber)"
    coinLabel.fontSize = 100
    coinLabel.fontColor = SKColor.black
    coinLabel.zPosition = 1
    coinLabel.position = CGPoint(x: self.size.width/2, y: self.size.height*0.1)
    self.addChild(coinLabel)



}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


    for touch: AnyObject in touches {

        let pointOfTouch = touch.location(in: self)
        let nodeITapped = atPoint(pointOfTouch)

        if nodeITapped.name == "Main Menu" {


            let sceneToMoveTo = MainMenuScene(size: self.size)
            sceneToMoveTo.scaleMode = self.scaleMode
            let myTrasition = SKTransition.fade(withDuration: 0.5)
            self.view!.presentScene(sceneToMoveTo, transition: myTrasition)
        }

            if nodeITapped.name == "Life" {
                if coinNumber > 10 {
                lifeNumber += 1
                coinNumber -= 10
                defaults.set(coinNumber, forKey: "coinNumberSaved")
                defaults.set(lifeNumber, forKey: "lifeNumberSaved")


                return

                }


            }
        }


    }


}

Solution

  • Just update your text in your tap recognizer again to change it

    coinLabel.text = "Coins: \(coinNumber)"
    

    You will need to make sure you have a reference of your coinLabel outside of your init so you can manipulate it outside of it. At the top of your ShopScene you can do...

    class ShopScene: SKScene {
    var coinLabel = SKLabelNode()
    //your code
    }
    

    and then when you go to init in your didMove(to view:) func, do instead...

     coinLabel = SKLabelNode(fontNamed: "The Bold Font")
    

    That way you can have a reference of it throughout your code. Although in the future you will want to think about making a subclass of SKNode to handle all your UI elements so you don't init everything in your GameScene.