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

时间:2014-11-25 03:07:18

标签: ios swift touchesbegan

我是Xcode的新手,想检查我如何编码,以便我可以使用触摸仅在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()
    }
}

我曾尝试在网上搜索一些链接,但我似乎并不太了解它。

1 个答案:

答案 0 :(得分:0)

如果你只是试图沿着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)

            // 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()
    }
}

查看CGGeometry上的文档,特别是CGPoint