我创建了一个gamecene,它包含两个节点,其中一个节点是后台节点。但是当调用touchesbegan命令时,注册的节点是后台节点,而不是我需要移动的节点。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches{
let touchLocation = touch.locationInNode(self)
let touchedNode = nodeAtPoint(touchLocation)
touchedNode.position = touchLocation
}
}
答案 0 :(得分:0)
以下是如何允许用户拖放节点的示例:
let background = SKSpriteNode(color:SKColor.blueColor(),size:CGSizeMake(200, 200))
let player = SKSpriteNode(color: SKColor.whiteColor(), size: CGSizeMake(32, 32))
var selectedNode:SKNode?
override func didMoveToView(view: SKView) {
let point = CGPointMake (CGRectGetWidth(view.frame)/2,CGRectGetHeight(view.frame)/2)
background.position = point
background.zPosition = 0
self.addChild(background)
// Give the node a name so we can identify it in the touch handler
player.name = "player"
// Ensure that the player rendered over the background
player.zPosition = 1
player.position = point
self.addChild(player)
}
选择用户触摸的节点
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches{
let touchLocation = touch.locationInNode(self)
let touchedNode = nodeAtPoint(touchLocation)
// Only allow the user to select "player" nodes
if (touchedNode.name == "player") {
selectedNode = touchedNode;
}
}
}
触摸位置改变时更新所选节点的位置
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches{
let touchLocation = touch.locationInNode(self)
if (selectedNode != nil) {
selectedNode!.position = touchLocation
}
}
}
触摸结束时取消选择所选节点
override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
selectedNode = nil;
}