【问题标题】:How to apply force on collision in iOS swift using sprite kit如何使用精灵套件在 iOS swift 中对碰撞施加力
【发布时间】:2014-09-20 04:05:16
【问题描述】:

我有这样的事情:

func didBeginContact(contact: SKPhysicsContact) {
  var paddle = getPaddleFromContact(contact) //of type SKPhysicsBody
  var ball = getBallFromContact(contact) //of type SKPhysicsBody
  if(paddle != nil && ball != nil){
   //apply force to ball depending on the speed/velocity of the paddle 
   //NEED HELP WITH THIS LOGIC
  }
}

目前用户可以通过触摸来拖动桨。它只能水平移动。我不确定哪个属性或如何根据桨的速度确定对球施加多少力。有关如何正确执行此操作的任何建议?

我尝试使用 paddle.velocity 但这些只是 0

【问题讨论】:

    标签: ios swift sprite-kit game-physics


    【解决方案1】:

    我目前正在做类似的事情。我有我自己的问题,但我有它的工作。我这样做的方法是使用触摸功能:

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    
       let touch = touches.anyObject() as UITouch!;
       self.firstLocation = touch.locationInNode(self);
    
    }
    
    override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
    
       if (nil == self.firstLocation) {
           // this is reset to nil in your collision function
           return;
       }
    
       let touch = touches.anyObject() as UITouch!;
       let location = touch.locationInNode(self);
    
       // use this vector in your collision function
       self.vector = CGVectorMake(self.firstLocation.x - location.x, self.firstLocation.y - location.y);
    
    }
    

    如果你的碰撞函数使用类似的东西:

    ball.applyImpulse(self.vector);
    

    免责声明:尚未尝试编译上述内容,因此可能需要调整解包选项等,显然还有 YMMV。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多