2014-11-25 67 views
0

我是Xcode的新手,想检查一下如何对它进行编码,以便我可以使用touch仅在y坐标中移动我的对象。如何使用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() 
    } 
} 

我尝试过在网上搜索几个链接,但我似乎并没有很好地理解它。

回答

0

如果你只是想从它的当前x位置沿y轴移动的“鸟”向上和向下,然后从该位置仅使用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

+0

嗨,感谢您的快速rpely(: 它的工作! – 2014-12-02 07:35:23

+0

@LeeBoXian没问题,你可以标记这是你的答案,如果它解决了这个问题? – bjtitus 2014-12-02 14:56:35

相关问题