Search code examples
iosswiftsprite-kitmaze

Is there a simple way to temporarily disable the physics bodies for a sprite in Swift? (SpriteKit)


I'm making a maze app, that generates a new maze after one is completed. Obviously, I can turn the walls (each having their own physics and sprite stored in their spot on an array) invisible by doing what I did in my code wall[location].isHidden = true ,but I couldn't find a way to do the same with the physics body bound to the wall, creating an invisible wall. Is there a simple way to do that without setting it to nil?

Just to clarify, it's not that I don't want it to be dynamic, I just want the entire thing to not exist while that maze is being solved. Also the only reason I wouldn't want to set it to nil is because I don't know how to restore its old physics body in an easy way.


Solution

  • The preferred way would be to physically remove your wall from your node tree. This will increase performance since the system does not have to check unnecessary nodes.

    Just make sure that your wall is being retained when you remove it so that you can readd it when needed.

    wall[location].removeFromParent()    
    

    ...

    scene.addChild(wall[location])