这是我的didBeginContact:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody = contact.bodyA
let secondBody = contact.bodyB
let ballWasContacted = firstBody.categoryBitMask == PhysicsCat.Ball || secondBody.categoryBitMask == PhysicsCat.Ball
let wallWasContacted = firstBody.categoryBitMask == PhysicsCat.Wall || secondBody.categoryBitMask == PhysicsCat.Wall
let wallCheckWasContacted = firstBody.categoryBitMask == PhysicsCat.wallSpace || secondBody.categoryBitMask == PhysicsCat.wallSpace
let scoreWasContacted = firstBody.categoryBitMask == PhysicsCat.Score || secondBody.categoryBitMask == PhysicsCat.Score
let boundaryWasContacted = firstBody.categoryBitMask == PhysicsCat.Boundaries || secondBody.categoryBitMask == PhysicsCat.Boundaries
if boundaryWasContacted {
print("I collided with the boundary.")
}
这是我的边界精灵节点:
let boundary = SKSpriteNode(color: SKColor.whiteColor(), size: CGSize(width: 50, height: 150))
boundary.name = "boundary"
boundary.position = CGPoint(x: self.frame.width / 1.37, y: self.frame.height / 2)
boundary.physicsBody = SKPhysicsBody(rectangleOfSize: boundary.size)
boundary.physicsBody!.categoryBitMask = PhysicsCat.Boundaries
boundary.physicsBody?.contactTestBitMask = PhysicsCat.Ball
boundary.physicsBody?.collisionBitMask = PhysicsCat.Ball
boundary.physicsBody!.dynamic = false
boundary.physicsBody!.friction = 0
boundary.physicsBody!.restitution = 1
boundary.zPosition = 5
self.addChild(boundary)
最后这是我的球:
Ball = SKSpriteNode(imageNamed: "Ball")
Ball.size = CGSize(width: 38, height: 38)
Ball.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
Ball.physicsBody = SKPhysicsBody(circleOfRadius: Ball.frame.width / 2)
Ball.physicsBody?.categoryBitMask = PhysicsCat.Ball
Ball.physicsBody?.collisionBitMask = PhysicsCat.Wall
Ball.physicsBody?.contactTestBitMask = PhysicsCat.Wall | PhysicsCat.Score | PhysicsCat.wallSpace | PhysicsCat.Boundaries
Ball.physicsBody?.affectedByGravity = false
Ball.physicsBody?.dynamic = true
Ball.physicsBody!.allowsRotation = true
Ball.zRotation = CGFloat(M_PI)
self.addChild(Ball)
当我接触到边界时,它打印出来"我与边界相撞#34;但我直接穿过它。我在代码中究竟缺少什么才能在球和边界之间发生实际的碰撞?
答案 0 :(得分:1)
改变你所做的事情联系,以便在接触时做点什么。
此问题已被提出,此线程中的解决方案是在您的didBeginContact中应用Impluse。
目前,您只会发生打印声明。 尝试添加相应的代码,如此答案。