【问题标题】:How to apply a basic vector to physics body in Swift 2.0如何在 Swift 2.0 中将基本向量应用于物理体
【发布时间】:2015-07-21 23:53:18
【问题描述】:

我正在制作一个非常简单的太空游戏;我似乎无法让最后一行工作。理想情况下,我希望在触摸屏幕时让空间向上移动。这是我的代码:

func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch: AnyObject in touches {

        touch.locationInNode(self)

        spacey.physicsBody!.velocity = (CGVectorMake(0, 0))

        spacey.physicsBody!.applyImpulse(0, atPoint: 25)

有人知道怎么做吗?

【问题讨论】:

  • 你的观点应该是一个整数吗? spacey.physicsBody!.applyImpulse(0, atPoint: 25)
  • 我试过双倍的,它不起作用

标签: ios swift sprite physics


【解决方案1】:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
        _ = touch.locationInNode(self)

        spacey.physicsBody!.velocity = CGVectorMake(0, 0)
        spacey.physicsBody!.applyImpulse(CGVectorMake(0, 25))

解决方案是在 swift 2.0 中将触摸更改为使用 _

【讨论】:

  • _ = 在这种情况下完全没有任何作用。您将 applyImpulse(0, atPoint: 25) 更改为 applyImpulse(CGVectorMake(0, 25)) 解决了您的问题;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多