Crashalot
Crashalot

Reputation: 34523

How to make sprites respond to contact events but not collision events (SpriteKit)?

Is it possible to make sprites in Swift respond only to contact events, ignoring collision events? We set the dynamic property to false for one of the sprites, but it didn't trigger the didBeginContact method when another sprite contacted it.

In other words, we want to get notified in didBeginContact when another sprite contacts an object, but we don't want the sprite to experience physics (e.g., no bouncing or moving on collision).

Upvotes: 0

Views: 560

Answers (1)

hamobi
hamobi

Reputation: 8130

Set collisionbitmask to 0 on both sprites. But set the contacttestbitmask to the opposite sprites categorybitmask

heres an example

heres the setup for my Power Up this is a physics body that doesnt exhibit physics behavior

    self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
    self.physicsBody!.categoryBitMask = CategoryPowerup
    self.physicsBody!.contactTestBitMask = CategoryShip
    self.physicsBody!.collisionBitMask = 0
    self.physicsBody!.dynamic = false

heres the setup for my Ship that gets the powerup

    let physicsBodyInset = CGRectInset(CGRectMake(0, 0, self.size.width, self.size.height), 2, 2)
    self.physicsBody = SKPhysicsBody(rectangleOfSize: physicsBodyInset.size)
    self.physicsBody!.restitution = 0
    self.physicsBody!.categoryBitMask = CategoryShip

Upvotes: 1

Related Questions