Detect collision between two objects in Swift

夙愿已清 提交于 2019-12-03 16:40:30

Here are a few items missing from your checklist:

  1. One of the physics bodies must be dynamic
  2. The physics bodies must be moved by a force/impulse or by setting their velocities
  3. The positions of the physics bodies should match their corresponding sprite/shape nodes

For (2), you are moving each bullet by changing its position over time with an SKAction.

For (3), the position of each bullet starts at (0, 0), while the shape is drawn at the top-center of the scene. Since the physics body is centered at the shape node's position, there is a mismatch between the locations of the shape and the physics body; the bullet's physics body never makes contact with the soldier. To resolve this, try

    var bullet = SKShapeNode(rectOfSize: CGSizeMake(10, 40))
    bullet.position = CGPointMake(self.size.width/2.0, self.size.height)

Also, the physics body of the soldier is half the radius of its shape.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!