我的触摸事件功能有问题,它说“严重错误:在展开可选值时意外发现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
编辑:
我唯一的精灵代码。当我单击它时,得到了解开的错误。
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)")
}