碰撞检测swift sprite kit。

时间:2015-05-05 22:40:16

标签: swift

我有两个障碍。我可以拖动它们中的一个(精灵)。我想要的是,当我拖动一个障碍并用它击中另一个障碍时,另一个将受到撞击的影响,并通过向另一个方向琢磨来遵循物理定律。

到目前为止,这是我的代码。

任何人都知道如何实现这一目标? 感谢

func addSprite() {
    sprite.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size.width / 2)
    sprite.position = CGPoint(x: size.width * 0.35, y: size.height * 0.5)
    sprite.physicsBody?.dynamic = false
    sprite.physicsBody?.allowsRotation = false
    sprite.physicsBody?.restitution = 1.0
    sprite.physicsBody?.categoryBitMask = UInt32(balCategory)
    sprite.physicsBody?.contactTestBitMask = UInt32(obstacleCategory)
    sprite.physicsBody?.friction = 0.0
    sprite.physicsBody?.angularDamping = 0.0
    sprite.physicsBody?.linearDamping = 0.0
    sprite.physicsBody?.usesPreciseCollisionDetection = true
    sprite.physicsBody?.collisionBitMask = 0
    addChild(sprite)
}
func addObstacle() {
    treat.setScale(0.5)
    treat.physicsBody = SKPhysicsBody(rectangleOfSize: treat.size)
    treat.physicsBody?.dynamic = false
    treat.physicsBody?.categoryBitMask = UInt32(obstacleCategory)
    treat.physicsBody?.contactTestBitMask = UInt32(balCategory)
    treat.physicsBody?.collisionBitMask = 0
    treat.physicsBody?.usesPreciseCollisionDetection = true
    treat.name = "treat"

    treat.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
    self.addChild(treat)
}

1 个答案:

答案 0 :(得分:0)

您要做的就是将sprite.physicsBody?.collisionBitMask = 0treat.physicsBody?.collisionBitMask = 0切换为sprite.physicsBody?.collisionBitMask = UInt32(obstacleCategory)作为您的第一个功能treat.physicsBody?.collisionBitMask = UInt32(balCategory)

将此值设置为零会使它在碰撞时不与物理相互作用,请告诉我这是否有效。