如何有效地将SKS中的子节点与场景中的代码链接起来

时间:2017-06-28 09:59:31

标签: ios swift sprite-kit skspritenode

我正在使用侧滚动游戏,我目前正在将我的节点从sks链接到类似的类。但是,我确信必须有一种更清晰,更有效的方法来实现这一点,因为我可以看到这个列表变得越来越大,添加了更多节点。将节点与类链接起来的有效方法是什么?在此先感谢您的帮助!

    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
    }

1 个答案:

答案 0 :(得分:2)

如果所有这些属性都是游戏的基本组成部分,并且确实需要将它们定义为属性,那么您可以跳过对Nil的检查,因为如果它们不在您的.sks文件中,然后出现了问题,所以你可能希望游戏崩溃。

您通常不需要按钮的属性,因为您可以按如下方式检查touchesBegan()中已按下哪个按钮:

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

这应该简化您的代码如下:

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

看起来不太糟糕。