Ios 如何使用触摸仅在y坐标中移动对象?

Ios 如何使用触摸仅在y坐标中移动对象?,ios,swift,touchesbegan,Ios,Swift,Touchesbegan,我是Xcode新手,希望检查如何对其进行编码,以便我可以使用touch仅在y坐标中移动对象 目前,我已将其编程为跟随我的触摸,但它将同时跟随x和y坐标 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { /* Called when a touch begins */ if moving.speed > 0 { for touch: AnyObject in touc

我是Xcode新手,希望检查如何对其进行编码,以便我可以使用touch仅在y坐标中移动对象

目前,我已将其编程为跟随我的触摸,但它将同时跟随x和y坐标

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    if moving.speed > 0  {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)


            bird.physicsBody?.velocity = CGVectorMake(0, 0)
            bird.position = location

        }
    }else if canRestart {
        self.resetScene()
    }
}

我试过在网上搜索一些链接,但我似乎不太了解。如果您只是想从当前x位置沿y轴上下移动“bird”,请仅使用该位置的y值:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */
    if moving.speed > 0  {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            // Just duplicate our old position
            var newPosition = bird.position
            // and change only the y value, leaving the x value as whatever it was initially set to. X will never change
            newPosition.y = location.y

            bird.physicsBody?.velocity = CGVectorMake(0, 0)
            bird.position = newPosition

        }
    }else if canRestart {
        self.resetScene()
    }
}

请查看和的文档。

@LeeBoXian没问题,如果它解决了问题,您能将此标记为您的答案吗?