如何使用场景中的代码高效地 link 来自 SKS 的子节点
How to efficiently link child nodes from the SKS with code in the scene
我正在开发横向卷轴游戏,我目前正在使用 类 从 sks link 获取我的节点,就像这样。但是,我确信必须有一种更清洁更有效的方法来执行此操作,因为我可以看到这个列表随着添加的节点越来越多而变得越来越庞大。使用 类 link up 节点的更有效方法是什么?在此先感谢您的帮助!
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()
中哪个按钮被'pressed',如下所示:
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
看起来还不错。
我正在开发横向卷轴游戏,我目前正在使用 类 从 sks link 获取我的节点,就像这样。但是,我确信必须有一种更清洁更有效的方法来执行此操作,因为我可以看到这个列表随着添加的节点越来越多而变得越来越庞大。使用 类 link up 节点的更有效方法是什么?在此先感谢您的帮助!
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()
中哪个按钮被'pressed',如下所示:
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
看起来还不错。