Search code examples
iosswiftsprite-kitskspritenode

How to efficiently link child nodes from the SKS with code in the scene


I'm working on a side scroller game, I'm currently linking my nodes from the sks with classes like so. However, I'm sure there must be a cleaner more efficient way to do this as I can see this list getting colossal with more nodes added. What is a more efficient way to link up nodes with classes? Thanks in advance for the help !

    if (self.childNode(withName: "TheCamera") != nil){
        theCamera = self.childNode(withName: "TheCamera") as! SKCameraNode
        self.camera = theCamera
    }

    if (self.childNode(withName: "button") != nil){
        button = self.childNode(withName: "button") as! SKSpriteNode
    }

    if (self.childNode(withName: "shootButton") != nil){
        shootButton = self.childNode(withName: "shootButton") as! SKSpriteNode
    }

    if (self.childNode(withName: "leftButton") != nil){
        leftButton = self.childNode(withName: "leftButton") as! SKSpriteNode
    }

    if (self.childNode(withName: "rightButton") != nil){
        rightButton = self.childNode(withName: "rightButton") as! SKSpriteNode
    }

    if (self.childNode(withName: "Key") != nil) {
        theKey = self.childNode(withName: "Key") as! Key
        theKey.setUpKey()
    }
    if (self.childNode(withName: "lifeBar") != nil) {
        theLifeBar = self.childNode(withName: "lifeBar") as! LifeBar
        theLifeBar.setUp()
    }

    if (self.childNode(withName: "health")) != nil {
        theHealthPack = self.childNode(withName: "health") as! HealthPack
        theHealthPack.setUp()
    }

    if (self.childNode(withName: "Weapon") != nil) {
        theWeapon = self.childNode(withName: "Weapon") as! Weapon
        theWeapon.setUpWeapon()
    }

    if (self.childNode(withName: "Weapon2") != nil) {
        theWeapon = self.childNode(withName: "Weapon2") as! Weapon
        theWeapon.setUpWeapon()
    }


    if (self.childNode(withName: "knife_count") != nil) {
        knife_count = self.childNode(withName: "knife_count") as! SKLabelNode
    }

Solution

  • If all of these properties are a fundamental part of your game, and you do indeed need them defined as properties, then you can skip the check for Nil, because if they are not in your .sks file, then something has gone wrong, so you might want the game to crash.

    You usually don't need a property for buttons, because you can check which button has been 'pressed' in touchesBegan() as follows:

        let touchLocation = touches.first!.location(in: self)
            if let touchedNode = self.nodes(at: touchLocation).first as SKSpriteNode
               {
                if let nodeName = touchedNode.name {
                    switch nodeName {
                    case "shootButton":
                        firePlayerMissile()
    
                    case "leftButton":
                        ship.moveLeft()
    
                    case "rightButton":
                        ship.moveRight()
    
                    case "pauseButton":
                        pauseGame()
    
                    default :
                        break
                }
            }
    

    This should get your code simplified as follows:

    self.camera = self.childNode(withName: "TheCamera") as! SKCameraNode
    
    theKey = self.childNode(withName: "Key") as! Key
    theKey.setUpKey()
    
    theLifeBar = self.childNode(withName: "lifeBar") as! LifeBar
    theLifeBar.setUp()
    
    theHealthPack = self.childNode(withName: "health") as! HealthPack
    theHealthPack.setUp()
    
    theWeapon = self.childNode(withName: "Weapon") as! Weapon
    theWeapon.setUpWeapon()
    
    theWeapon = self.childNode(withName: "Weapon2") as! Weapon
    theWeapon.setUpWeapon()
    
    knife_count = self.childNode(withName: "knife_count") as! SKLabelNode
    

    which doesn't look too bad.