我在SKScene这样的代码:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
var touch: AnyObject = touches.anyObject()
var point = getPoint(touch.locationInNode(self))
var name = NSStringFromCGPoint(point)
for children in self.children {
if (children as SKSpriteNode).name == name {
println("exist!")
}
}
var tempNode = self.childNodeWithName(name)
}
我看“存在!” 在日志中,所以用这个名字在孩子阵列中的节点,但tempNode 为零 。 该self.childNodeWithName("//" + name)
调用返回nil了。
以下是如何在斯威夫特做到这一点......希望这有助于!
var mySprite: SKSpriteNode = childNodeWithName("mySprite") as SKSpriteNode
我发现使用雨燕2.2这个陌生感,也许是错误..你不能使用NSStringFromCGPoint和childNodeWithName不清洗从括号中的字符串:
使用这种方法一点:
func removeBraces(s:String)->String {
return s.stringByTrimmingCharactersInSet(NSCharacterSet.init(charactersInString: "{}"))
}
当您添加SKSpriteNode比如做:
...
mySpriteNode.name = removeBraces(NSStringFromCGPoint(mySpriteNode.position))
...
并检索它为您的情况:
override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {
var touch: AnyObject = touches.anyObject()
var point = getPoint(touch.locationInNode(self))
var name = removeBraces(NSStringFromCGPoint(point))
if let child = self.childNodeWithName(name) {
print("I've found: \(child)")
}
...
}