I am trying to detect contact between two nodes in SpriteKit with Swift, but when contact was made, I got unexpected value for the categoryBitMask. With the following block of code, I should expected to get the value of 1 or 2 for bodyA, 3 or 4 for bodyB, instead of that, I got the value 1 or 2 for bodyA, but I got a value of 4294967295 for bodyB. Any ideas why this happened? and how I can fix it? Thanks.
func testAddingSprites() {
var fallSprite = SKSpriteNode()
let fallingShapeIndex = Int(arc4random_uniform(UInt32(fallingShape.count)))
switch fallingShapeIndex {
case 0:
fallSprite = SKSpriteNode(imageNamed: "rectangle.png")
fallObj.append(fallSprite)
println(fallObj.count, fallingShapeIndex)
fallSprite.physicsBody?.categoryBitMask = 3
fallSprite.physicsBody?.contactTestBitMask = 1 | 2
case 1:
fallSprite = SKSpriteNode(imageNamed: "circle.png")
fallObj.append(fallSprite)
println(fallObj.count,fallingShapeIndex)
fallSprite.physicsBody?.categoryBitMask = 4
fallSprite.physicsBody?.contactTestBitMask = 1 | 2
default:
println("Unexpected fallingShapeIndex", fallingShapeIndex)
}
fallSprite.physicsBody = SKPhysicsBody(circleOfRadius: 50)
fallSprite.position = CGPointMake(self.frame.width / 2, self.frame.height / 2 + 250)
fallSprite.physicsBody?.dynamic = true
fallSprite.physicsBody?.affectedByGravity = false
self.addChild(fallSprite)
// set fall object movment
let moveAction = SKAction.moveTo(CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) - 110), duration: 3)
let cleanAction = SKAction.removeFromParent()
fallSprite.runAction(SKAction.sequence([moveAction, cleanAction]), withKey: "fallObjMove")
}
Here are some solutions I already tried,
-
I tried to assign the categoryBitsMask by the following code, I got the same unexpected value.
enum fallObjGroup:UInt32 { case rectangleFall = 1 case circleFall = 2 } enum matchObjGroup:UInt32 { case rectangleMatch = 1 case circleMatch = 2 } -
I tried to clean build folder, and restart Xcode, no luck either.
EDIT: code for getting the value:
func didBeginContact(contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == 1 && contact.bodyB.categoryBitMask == 3 {
println("rectangle contact made", Int(contact.bodyA.categoryBitMask),Int(contact.bodyB.categoryBitMask))
} else if contact.bodyA.categoryBitMask == 2 && contact.bodyB.categoryBitMask == 4 {
println("circle contact made", Int(contact.bodyA.categoryBitMask),Int(contact.bodyB.categoryBitMask))
} else {
println("GAME OVER!!!", Int(contact.bodyA.categoryBitMask),Int(contact.bodyB.categoryBitMask))
}
}
Result: (GAME OVER!!!, 1, 4294967295) & (GAME OVER!!!, 2, 4294967295)
Aucun commentaire:
Enregistrer un commentaire