简体   繁体   中英

Removing all Physics from a Node in Spritekit Swift

I am trying to remove all physics from a node, so that it has no physical proporties at all. I want it to act how it would act if I had just added the node without attaching a physics body. I have tried using the .dynamic property and I have found that this makes it so that no force can be applied to that node, but other nodes can still colide with it.

Basicly what I am trying to do is remove the physics body from a node.

I am uising spritekit and swift, thank you in advance.

Let's assume you have a node called 'character' that's set up like this.

    self.character.physicsBody = SKPhysicsBody(texture: firstFrame, size: characterSize)
    self.character.physicsBody?.affectedByGravity = true
    self.character.physicsBody?.affectedByGravity = true
    self.character.physicsBody?.allowsRotation = false

You can change node's physicsBody to this:

    self.character.physicsBody = nil

All the above mentioned physics will be removed. You can also just change one or two attributes of the physicsBody like you said in your question. For example, you can change the collision category to prevent objects from colliding if this is what you want for your situation.

    self.character.physicsBody?.categoryBitMask = colliderType.None.rawValue

If you try this make sure you have a 'none' category set up. This can be done like so.

    enum colliderType:UInt32 {
        case Character = 1
        case Block = 2
        case Wave = 3
        case None = 4
    }

What I did was this: I changed the categoryBitMask of the character to 0:

self.physicsBody.categoryBitMask = 0;

This did the trick and after doing this, my character would not register any contact.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM