1

I'm coding a SpriteKit game and I'm trying to add the equivalent of a Cocos2D sensor. My problem is that my sensor keeps pushing the other sprites, but this isn't what I want. I want the sensor to be able to overlap with other sprites and not push them around. How do I fix this? Here is the code I've added for my sensor:

bubble.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(50))
bubble.physicsBody?.dynamic = false
bubble.physicsBody?.allowsRotation = false
bubble.physicsBody?.affectedByGravity = false

And here is my collision method:

func didBeginContact(contact: SKPhysicsContact) {
    let collision:UInt32 = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask)

    if collision == (playerCategory | crateCategory) {
        NSLog("Game Over")
        var myLabel = SKLabelNode(fontNamed: "Arial")
        myLabel.text = "GAME OVER"
        myLabel.fontSize = 50
        myLabel.color = SKColor.blueColor()
        myLabel.position = (CGPointMake(self.frame.size.width*0.5, self.frame.size.height*0.5))
        self.addChild(myLabel)
        fisherman.physicsBody?.dynamic = false
        fisherman.removeFromParent()
        fish.physicsBody?.dynamic = false
        fish.removeFromParent()
        crate.physicsBody?.dynamic = false
        crate.removeFromParent()
        crateDuplicate.physicsBody?.dynamic = false
        crateDuplicate.removeFromParent()


    }
    if collision == (playerCategory | bubbleCategory) {
        NSLog("Bubble Contact")
        bubbleDuplicate.removeAllActions()
        bubbleDuplicate.removeFromParent()
        bubble.removeAllActions()
        bubble.removeFromParent()
        bubbleDuplicate.setScale(0)
        bubble.setScale(0)
        bubbleDuplicate.alpha = 0


               }


}

1 Answer 1

3

Set a collisionBitMask to 0, so all collisions of this particular object to others will be ignored.

18
  • @kriskendall99 you need to make object dynamic. Static objects still affect bouncing of other objects but ignores collisionBitMask. Commented Jun 30, 2015 at 23:22
  • Do you have any additional behaviors in contact delegate implemented? Contact still should happen, collision should not. How do you verify that it's still not working? Commented Jun 30, 2015 at 23:34
  • Yes, I have additional behaviors implemented. I've added a method to enable collisions with enemy sprites. I know it's not working because, when I run the game, collisions are still being made. My character sprites are able to jump on top of the sprite that's supposed to have collisions disabled. Commented Jun 30, 2015 at 23:40
  • Could you post your custom collision method as well? Thanks. Commented Jun 30, 2015 at 23:41
  • 1
    Well, I added these lines fish.physicsBody?.collisionBitMask = playerCategory | crateCategory fish.physicsBody?.contactTestBitMask = playerCategory | crateCategory and now collisions with the bubble no longer occur! Thanks for the help! Commented Jul 1, 2015 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.