나는 내가 발견 한이 코드로 작업하고 있으며 내 객체의 노드 이름을 얻는 방법을 알아 내려고 노력하고 있습니까?SKSpriteNode 이름을 얻는 방법은 무엇입니까?
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´
입니다.
이것이 가능합니까?
추가 :
따라서, 후속, 내가 어떻게 터치가 내 개체 내에 있는지 여부를 감지 할 수 있습니까? 여기에 몇 가지 더 많은 코드입니다 : 터치 개체에 대해 않았거나 만약 내가 감지 할 (SKScene
에 테스트 할 수 없습니다, 작동되지 않음) if !contains(SKScene, node)
와
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))
}
}
}
}
외부 .. 외부 만약 (터치 SKScene입니다) 내가 프로그래밍의 모든 SKSpriteNode
을 할 계획입니다 있다는 것입니다 .. 아무것도 할
내가이 방법을하고 즉 sprite1
로 let
이름을 테스트 할 할 이유를 원하지 않는다, 그래서 필요하지 않습니다 노드 객체의 이름을 알아 두십시오 ..
어떤 제안이 있습니까?
좋아, 점점 가까워 지네 .-D if 문을 고치는 법을 찾아 냈어 :'if! contains ([self], node)'... 그거야! 그러나 그것은 일을하는 올바른 방법입니까? – Kjetil
또한'touchesEnded '아래의 코드를'touchesMoved'로 옮겼습니다. 그래서 이제 화면상의 터치에 따라 박스 (객체)가 부드럽게 움직입니다 :-D – Kjetil