gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:18:59 25 4
gpt4 key购买 nike

我正在开发一款横版卷轴游戏,我目前正在将我的节点从 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
}

最佳答案

如果所有这些属性都是您游戏的基本部分,并且您确实需要将它们定义为属性,那么您可以跳过对 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

看起来还不错。

关于ios - 如何有效地将 SKS 的子节点与场景中的代码链接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44799349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com