我正在使用我找到的代码并尝试找出如何获取我的对象的节点名称?
这是代码的一部分:
let sprite1 = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: 30, height: 30))
let sprite2 = SKSpriteNode(color: UIColor.greenColor(), size: CGSize(width: 30, height: 30))
let sprite3 = SKSpriteNode(color: UIColor.blueColor(), size: CGSize(width: 30, height: 30))
let sprite4 = SKSpriteNode(color: UIColor.yellowColor(), size: CGSize(width: 30, height: 30))
var selected: [UITouch: SKNode] = [:]
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
selected = [:]
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
selected[touch as UITouch] = nodeAtPoint(location)
println(self.name)
}
}
我想要实现的是获得println
返回SKSprintNode
名称(sprite1、sprite2、sprite3 或 sprite4)...我已经尝试了几种方法,但我得到的只是´nil´
.
这可能吗?
Added:
那么,接下来,我如何检测触摸是否在我的对象内?这是更多代码:
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
for (touch, node) in selected{
if !contains(SKScene, node){
let action = SKAction.moveTo(location, duration: 0.1)
node.runAction(SKAction.repeatAction(action, count: 1))
}
}
}
}
随着if !contains(SKScene, node)
(这不起作用,无法测试SKScene
)我想检测触摸是在物体上还是在外面。如果在外面(触摸是 SKScene)我不想做任何事情。
我想这样做而不是测试的原因let
名称为 i.esprite1
是我打算做所有SKSpriteNode
以编程方式,所以我不需要知道节点对象的名称。
有什么建议么?