gpt4 book ai didi

ios - 在展开可选值时发现 nil - spriteKit

转载 作者:搜寻专家 更新时间:2023-11-01 05:43:14 26 4
gpt4 key购买 nike

我的触摸事件函数有问题,它说“ fatal error :在展开可选值时意外发现 nil”我正在尝试单击标签下的节点。

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let location: CGPoint = touches.anyObject().locationInNode(self)
let nodes: NSArray = nodesAtPoint(location)

for node: AnyObject in nodes {
println("node name : " + node.name) // Error here
}

错误:

fatal error: unexpectedly found nil while unwrapping an Optional value

控制台输出:

node name : myLabel
node name : MyNode

问题只出现在我的设备上,它在模拟器上完美运行。Xcode 6.4、SDK 8.0、目标 7.0、设备 7.1.2

编辑:

场景中我唯一的 Sprite 代码。单击它时出现展开错误。

   var score: SKLabelNode = SKLabelNode()
score.text = "0"
score.name = "scoreLabel"
score.fontName = "Chalkduster"
score.fontSize = 42
score.zPosition = 100
score.fontColor = UIColor.redColor()
score.position = CGPointMake(CGRectGetWidth(self.frame) - 50, CGRectGetHeight(self.frame) - 40)
self.addChild(score)

最佳答案

你必须尝试一下设备:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
let location: CGPoint = touches.anyObject().locationInNode(self)
let nodes: NSArray = nodesAtPoint(location)

for node: AnyObject in nodes {
let strName = node.name as string
println("node name : \(strName)")
}

关于ios - 在展开可选值时发现 nil - spriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24983177/

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