locationInNode 返回错误结果

locationInNode returning erroneous results

我有一个名为 Player 的 SKNode 子类,它几乎包含以下内容:http://hub.ae/blog/2014/03/26/soft-body-physics-jellyusing-spritekit/ 转换为 swift(有一些更改)。我允许用户使用以下代码用手指移动播放器节点:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    player.runAction(SKAction.moveTo(touch.locationInNode(world), duration: 1))
}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    player.runAction(SKAction.moveTo(touch.locationInNode(world), duration: 1))
}

override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
    player.removeAllActions()
}

override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    player.removeAllActions()
}

*请注意,您看到的 'world' var 只是另一个 SKNode。 'player' 是该节点的子节点。

这看起来应该可行,但是节点向非常奇怪的方向移动。这是它的外观示例: http://gyazo.com/87b0d09101bbbfd3ac0f2a3cdbf42e4c

我该如何解决这个问题?我发现将场景的锚点设置为 0.5 可以解决这个问题,但是 'player' 节点的物理体会变得混乱。

我遇到了同样的问题,但我不记得我是不是这样解决的。尝试改变这个:

world > self

player.runAction(SKAction.moveTo(touch.locationInNode(world), duration: 1))

收件人:

player.runAction(SKAction.moveTo(touch.locationInNode(self), duration: 1))

如果我错了请告诉我:)

编辑

将锚点设置为 (0.5,0.5) 并将这 2 个方法写入以您的节点为中心。

override func didSimulatePhysics() {
    camera.position = CGPointMake(player.position.x, player.position.y);
    centerOnNode(camera)
}

func centerOnNode(node: SKNode){
    var positionInScene : CGPoint = convertPoint(node.position, fromNode: node.parent)
    world.position = CGPointMake(world.position.x - positionInScene.x, world.position.y - positionInScene.y)
}