我的代码检测到与精灵的碰撞但没有发生碰撞? - Swift
My code detects collisions with a sprite but doesn't collide? - Swift
这是我的 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)
当我接触到边界时,它打印"I collided with the boundary",但我直接穿过它。我的代码中到底缺少什么才能使球和边界之间发生实际碰撞?
将您的 didBeginContact 更改为在联系时做一些事情。
有人问过这个问题,这个线程中的解决方案是在您的 didBeginContact 中应用 Impluse。
目前,您只有打印语句。
尝试添加此答案中的相关代码。
这是我的 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)
当我接触到边界时,它打印"I collided with the boundary",但我直接穿过它。我的代码中到底缺少什么才能使球和边界之间发生实际碰撞?
将您的 didBeginContact 更改为在联系时做一些事情。
有人问过这个问题,这个线程中的解决方案是在您的 didBeginContact 中应用 Impluse。
目前,您只有打印语句。 尝试添加此答案中的相关代码。