【发布时间】:2015-02-15 13:45:01
【问题描述】:
当球与杯子碰撞时,分数应该增加 1。但目前它会增加 1、3 或有时是 4,这意味着多次检测到碰撞。
我想我一定是错误地检查了碰撞:
let ballCategory : UInt32 = 0x1 << 0
let cupCategory : UInt32 = 0x1 << 1
在我的didMoveToView:
//Set physicsBody of scene to a physics body that borders the screen
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
physicsWorld.contactDelegate = self
//Ball
ballSprite.physicsBody = SKPhysicsBody(rectangleOfSize: ballSprite.size)
ballSprite.physicsBody!.categoryBitMask = ballCategory
ballSprite.physicsBody!.contactTestBitMask = cupCategory
//Cup
cupSprite.physicsBody = SKPhysicsBody(edgeLoopFromRect: cupSprite.size)
cupSprite.physicsBody!.categoryBitMask = cupCategory
cupSprite.physicsBody!.contactTestBitMask = ballCategory
在我的didBeginContact:
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
//Assign the two physics bodies so that the one with the lower category is always stored in firstBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
}
else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
//contact between ball and cup
if firstBody.categoryBitMask == ballCategory && secondBody.categoryBitMask == cupCategory {
score++
println("point scored")
moveBall() //Move ball back to start position
}
这在它正在检测碰撞的意义上是有效的,但是它会发生多次,而它应该只发生一次。
【问题讨论】:
-
确保您拥有完全独立的对象类别。
标签: swift sprite-kit skphysicsbody