解开 node.name.last() 时查找 nil



我在下面的代码中遇到了问题,我迫切需要我按下的节点名称的最后一个字母。

为此,我循环访问场景的所有子节点,直到找到与触摸点共享位置的子节点。

一旦我发现了这一点,我就使用一个警卫状态:

let nodeName = node.name
guard let letter = nodeName?.last else { return }

查找字母,但编译器不会继续运行函数,因为它在尝试从节点名称中提取最后一个字符时会发现 nil。

感谢一百万对我和我的代码的耐心,因为我是初学者

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let touchPosition = touch.location(in: self)
guard let scene = scene else { return }
for node in scene.children {
if node.frame.contains(touchPosition) && node.name != "background" {
let nodeName = node.name
guard let letter = nodeName?.last else { return }
handleGuess(letter: letter)
node.removeFromParent()
if !(lettersToGuess?.contains(letter))! && letter != "1" && letter != "0" {
currentStep += 1
} else if letter == "1" {
} else if letter == "2" {
} else if letter == "3" {
} else if letter == "4" {
}
}
}
}
func setupMenu() {      
// Load Background and place background image
let backgroundTexture = SKTexture(imageNamed: "menuBG")
background.texture = backgroundTexture
background.zPosition = -1
background.position = CGPoint(x: frame.midX * 0.25, y: frame.midY)
let bgSizeFactor = backgroundTexture.size().height / frame.size.height
background.size = CGSize(width: backgroundTexture.size().width / bgSizeFactor , height: frame.size.height)
addChild(background)
animateBackground()
setupRain()
// TODO: Add Button center screen displaying current level, clicking this will start a new game.
let playTexture = SKTexture(imageNamed: "playButton")
let playButton = SKSpriteNode(texture: playTexture)
playButton.name = "playbutton2"
let playButtonFactor = playTexture.size().width / frame.size.width
playButton.size = CGSize(width: frame.size.width * 0.65, height: (playTexture.size().height / playButtonFactor) * 0.65)
playButton.position = CGPoint(x: frame.midX, y: frame.midY)
playButton.zPosition = 1
addChild(playButton)
let rateTexture = SKTexture(imageNamed: "rateButton")
let rateButton = SKSpriteNode(texture: rateTexture)
rateButton.name = "rateButton3"
rateButton.size = CGSize(width: playButton.size.height * 0.85, height: playButton.size.height * 0.85)
rateButton.position = CGPoint(x: frame.midX, y: frame.midY - playButton.size.height)
rateButton.zPosition = 1
addChild(rateButton)
let noAdsTexture = SKTexture(imageNamed: "noAdsButton")
let noAdsButton = SKSpriteNode(texture: noAdsTexture)
noAdsButton.name = "noAdsButton4"
noAdsButton.size = CGSize(width: playButton.size.height * 0.85, height: playButton.size.height * 0.85)
noAdsButton.position = CGPoint(x: frame.midX - playButton.size.height, y: frame.midY - playButton.size.height)
noAdsButton.zPosition = 1
addChild(noAdsButton)
let zapTexture = SKTexture(imageNamed: "zapButton")
let zapButton = SKSpriteNode(texture: zapTexture)
zapButton.name = "zapButton5"
zapButton.size = CGSize(width: playButton.size.height * 0.85, height: playButton.size.height * 0.85)
zapButton.position = CGPoint(x: frame.midX + playButton.size.height, y: frame.midY - playButton.size.height)
zapButton.zPosition = 1
addChild(zapButton)
}

我认为你的下行是问题所在

guard let letter = nodeName?.last else { return }

您返回此处会中断 for 循环并从该方法返回。 相反,请尝试将其替换为以下内容。

guard let letter = nodeName?.last else { continue }

如果当前节点的字母为 nil,则上面将继续循环。

最新更新