Hello.
I have a multiple collision problem. There is a bullet, which hits the enemy(red rectangle). Then, it ++ the score. There is a spiral (red circle) which is supossed to trigger the scene to end when the enemy (red rectangle) touches it.
In this situation, when enemy hits the spiral, it works, the scene ends, and we go to the menu screen. But, when bullet hits the enemy, the same thing happens, and I don't know why.
Now, here's my code:
struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let enemyOne : UInt32 = 0b1
static let enemyTwo : UInt32 = 0b1
static let bullet : UInt32 = 0b10
static let spiral : UInt32 = 0b111
}
spiral.physicsBody = SKPhysicsBody(rectangleOfSize: spiral.size)
spiral.physicsBody?.categoryBitMask = PhysicsCategory.spiral
spiral.physicsBody?.contactTestBitMask = PhysicsCategory.enemyOne
spiral.physicsBody?.collisionBitMask = PhysicsCategory.None
...
enemyOne.physicsBody = SKPhysicsBody(rectangleOfSize: enemyOne.size)
enemyOne.physicsBody?.dynamic = true
enemyOne.physicsBody?.categoryBitMask = PhysicsCategory.enemyOne
enemyOne.physicsBody?.contactTestBitMask = PhysicsCategory.bullet | PhysicsCategory.spiral
enemyOne.physicsBody?.collisionBitMask = PhysicsCategory.None
...
bullet.physicsBody = SKPhysicsBody(circleOfRadius: bullet.size.width / 2)
bullet.physicsBody?.dynamic = true
bullet.physicsBody?.categoryBitMask = PhysicsCategory.bullet
bullet.physicsBody?.contactTestBitMask = PhysicsCategory.enemyOne
bullet.physicsBody?.collisionBitMask = PhysicsCategory.None
bullet.physicsBody?.usesPreciseCollisionDetection = true
...
func bulletDidCollideWithEnemy(bullet: SKSpriteNode, enemyOne: SKSpriteNode) {
scoreOnScreen.text = String(score)
score++
bullet.removeFromParent()
enemyOne.removeFromParent()
}
func enemyDidCollideWithSpiral(enemyOne: SKSpriteNode, spiral: SKSpriteNode) {
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let skView = self.view! as SKView
let scene = MenuScene(size: skView.bounds.size)
scene.scaleMode = SKSceneScaleMode.AspectFill
skView.presentScene(scene, transition: SKTransition.crossFadeWithDuration(0.5))
}
// Did Begin Contact
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody
var secondBody : SKPhysicsBody
var thirdBody : SKPhysicsBody
var fourthBody : SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
thirdBody = contact.bodyA
fourthBody = contact.bodyB
} else {
thirdBody = contact.bodyB
fourthBody = contact.bodyA
}
if (firstBody.categoryBitMask & PhysicsCategory.enemyOne != 0) && (secondBody.categoryBitMask & PhysicsCategory.bullet != 0) {
bulletDidCollideWithEnemy(firstBody.node as SKSpriteNode, enemyOne : secondBody.node as SKSpriteNode)
}
if (thirdBody.categoryBitMask & PhysicsCategory.enemyOne != 0) && (fourthBody.categoryBitMask & PhysicsCategory.spiral != 0) {
enemyDidCollideWithSpiral(thirdBody.node as SKSpriteNode, spiral : fourthBody.node as SKSpriteNode)
}
Now, I know it's a mess, but can anyone help me? I think it the problem has to do with the bodyA.categoryBitMask and bodyB being set to different things even thought they are the same(?). I don't know. Anyone?