Marsgames Marsgames - 11 days ago 8
Swift Question

Remove physics body in Swift 3

Is there a way to remove a physics body in swift 3 ?

All I found was

myNode.phyisicsBody = nil
but it doesn't work in swift 3


"Nil cannot be assigned to type SKPhysicsBode"


In fact, by default my
scene.sks
has a child Scene who has a
physicsBody
, a small dot at the center, and it's this one I would remove

<code>The code to get the physicsBody</code>
<code>scene.sks with "Scene" child</code>
Small dot that I don't want

Answer

Just tried the following code in playground, you can myNode.physicsBody = nil

import UIKit
import SpriteKit

let spriteNode = SKSpriteNode(imageNamed: "elephant")

spriteNode.physicsBody = SKPhysicsBody(texture: spriteNode.texture!,
                                       size: spriteNode.texture!.size())
spriteNode.physicsBody = nil // No Error

From Apple API about physicsBody:

The default value is nil, which indicates that the node does not participate in the physics simulation at all. If a physics body is provided, when the scene’s physics are simulated, the physics body updates the node’s position and rotates the node.

I would say, check where you initially created the physicsBody, and make sure it is optional.

Edit

SceneObject!.physicsBody! = nil 

This cannot work! by using the ! you are telling the compiler that it is not nil

Change it to

SceneObject!.physicsBody = nil 
Comments