将物体移动到触摸位置

Moving object to location of touch

如何将 'player' 位置移动到用户触摸屏幕的位置?我希望他们能够通过点击不同的地方不断移动它。这是我目前所拥有的,但没有用:

class 游戏场景:SKScene {

let player = SKSpriteNode(imageNamed: "Spaceship")

override func didMoveToView(view: SKView) {
    // 2
    backgroundColor = SKColor.whiteColor()
    // 3
    player.position = CGPoint(x: size.width/2, y: size.height/2)

    // 4
    addChild(player)




}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)

        let player = SKSpriteNode(imageNamed:"Spaceship")
        player.position = location




    }
}

touchesBegan 中的 let player = SKSpriteNode(imageNamed:"Spaceship") 行中,您正在创建飞船节点的新实例。 该节点不在场景中,设置新节点的位置不会改变现有播放器节点的位置。

只需尝试重置玩家节点的位置即可。

尝试

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {

    let touch = touches.anyObject() as UITouch?
    if let location = touch?.locationInNode(self)
    {
        player.position = location
    }
}