我的SKScene有一个空节点(buttonsGroup)。在我的GameView类中,我创建了一个节点(ball1),它是空节点的父节点。
buttonsGroup = childNodeWithName("ball1")
var ball1 = SKSpriteNode(imageNamed: "ball")
ball1.name == "ball1"
buttonsGroup.addChild(ball1)当我试图使用TouchesBegan移动节点时,我的问题出现了:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let touchNode = nodeAtPoint(location)
if touchNode.name != "ground" {
touchNode.position = location
}
}
} 我已经知道,如果使用SKScene和touchNode位置,它们将返回不同的值(至少看起来是这样)。在这种情况下,节点就会跳到任何地方。我的SKScene的尺寸是480x320。
有人知道解决办法吗?如果我用手做(没有SKScene),它没有任何问题。在尝试使用moveBy操作或任何其他需要我在视图中的位置的操作时,我已经多次遇到了这个问题。
发布于 2015-08-26 21:24:59
如果您试图移动球,您将需要将触摸位置从场景坐标转换为使用转换点:toNode或转换点:从节点的球的父坐标。
例如,
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in (touches as! Set<UITouch>) {
let location = touch.locationInNode(self)
let node = nodeAtPoint(location)
if (node.name != "ground") {
let locationInNode = convertPoint(location, toNode:node.parent!)
node.position = locationInNode
}
}
}https://stackoverflow.com/questions/32235959
复制相似问题