Change position of a SKSpriteNode that has a physicsBody

前端 未结 7 2290
遇见更好的自我
遇见更好的自我 2021-02-13 22:02

I can\'t change the position of a SKSpriteNode by changing

self.position = newPosition;

It doesn\'t work anymore if it has a physicsBody.

7条回答
  •  無奈伤痛
    2021-02-13 22:28

    I ran into a similar problem. When using SKAction, even with duration set to 0.0 I got strange behaviours especially when two SKActions had been triggered at the same time.

    I tried setting position directly but as mentioned by others this doesn't work when using the SKPhysicsContactDelegate.

    However for me it worked to remove the node from its parent, I then set the new position, and other things I want to change, and then I add the node again to its former parent.

    It's not ideal but in some cases it might help.

    As an example with the SKPhysicsContactDelegate method didBegin:

    func didBegin(_ contact: SKPhysicsContact) {
    
        guard let node = contact.bodyB.node else { return }
    
        node.removeFromParent()
        node.position = CGPoint(x: 10, y: 10)
        addChild(node)
    }
    

提交回复
热议问题