我使用此代码的工作,我发现并试图找出如何让节点名称为我的对象?
下面是部分代码:
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´
。
这可能吗?
添加:
因此,要跟进,我怎么能检测触摸是否是我的对象之内? 这里的一些代码:
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
的名称作为即sprite1
是,我打算让所有SKSpriteNode
编程,所以我没有必要知道的节点对象的名称..
有什么建议么?