简体   繁体   English

在Spritekit Swift中从节点移除所有物理

[英]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. 我尝试使用.dynamic属性,但发现这样做可以使没有力施加到该节点上,但是其他节点仍然可以与其碰撞。

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. 我正在迅速使用spritekit,在此先感谢您。

Let's assume you have a node called 'character' that's set up like this. 假设您有一个名为“ character”的节点,其设置如下。

    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: 您可以将节点的physicsBody更改为此:

    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. 您也可以像在问题中所说的那样仅更改physicsBody一个或两个属性。 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: 我所做的是:我将字符的categoryBitMask更改为0:

self.physicsBody.categoryBitMask = 0;

This did the trick and after doing this, my character would not register any contact. 这样就成功了,这样做之后,我的角色将不会注册任何联系人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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