我的下面的代码有问题,我非常需要我按下的节点名称的最后一个字母。
为此,我迭代了场景中的所有子节点,直到找到一个与触摸点共享位置的子节点。
一旦发现这一点,我就会使用警卫声明:
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)
}
答案 0 :(得分:1)
我认为您的下一行是问题
guard let letter = nodeName?.last else { return }
您将返回此处,这会中断for循环并从方法中返回。 而是尝试将其替换为下面的内容。
guard let letter = nodeName?.last else { continue }
如果当前节点的字母为nil,则以上将继续循环。