tonethar
tonethar

Reputation: 2282

Resizing a SpriteKit SKPhysicsBody created with SKPhysicsBody(circleOfRadius:)

I am building a SpriteKit "chain reaction" game where circles get larger as they "explode". I am using the SKPhysicsBody(circleOfRadius:) initializer to create physics bodies for the circles.

Unfortunately, these physics bodies don't seem to re-size as the circles get larger, and they don't have an exposed .radius property I can set. Is there any way to resize a physics body in SpriteKit, or do I need to create a new physics body every frame instead? (This is what I have done below and the code works fine, but it feels really inefficient to create new physics bodies in update loops.)

PS - I know with circles I can easily do accurate collision detection using trig to get the distances, but I wanted to use SpriteKit collision detection for some other effects.

func moveCircles(dt:CGFloat){
   enumerateChildNodesWithName("circle") { node, stop in
     let c = node as! CircleSprite
     var halfWidth = c.size.width / 2.0
     c.update(dt) // in update() exploding circles get larger.
         if c.isExploding{
        // FIXME: Have to make a new physics body every update - I don't like this!
            c.physicsBody = SKPhysicsBody(circleOfRadius: halfWidth)
            c.physicsBody?.categoryBitMask = CollisionCategories.ExplodingCircle
            c.physicsBody?.contactTestBitMask = CollisionCategories.Circle
            c.physicsBody?.collisionBitMask = 0
            c.physicsBody?.dynamic = true // the default
        }
  }
}

Upvotes: 2

Views: 1657

Answers (1)

Wraithseeker
Wraithseeker

Reputation: 1904

You might want to read up more here. Resizing the physicsBody is not possible and it is recommended that you create a new physicsBody and assign it to the node.

Upvotes: 4

Related Questions