Swift Spritekit I am detecting collision but it reads collision reversal time

AppImage I have a wall of four rectangles that have different colors to go through the wall, the color of the ball must match that rectangle on the wall. The ball will go through the wall and a new wall will appear. However, when I detect this collision, I get multiple collision readings. I've tested this by printing dead or alive and it prints both many and many times.

func didBegin(_ contact: SKPhysicsContact) {

    if let nodeA = contact.bodyA.node as? SKShapeNode, let nodeB = contact.bodyB.node as? SKShapeNode {
        if nodeA.fillColor != nodeB.fillColor {
            print("DEAD")
        }
        else {
            print("Alive")
        }
    }
}      

      

Please, help!!!

+3


source to share


1 answer


Yes it does. The way to handle this (you can't get the spriteset to NOT be called didBegin

multiple times in some cases) is to make sure your contact code takes this into account and that handling the contract multiple times does not cause a problem (like adding to the invoice multiple times , deleting multiple lives, trying to access a node or physical block that was deleted, etc.).

Discussed here: Sprite-Kit logs multiple collisions for a single contact



Some things you can do include:

  • If you delete the related node, check that it is there nil

    before you delete it (for duplicate contacts)
  • Add a node to the set and then remove all the nodes in the set in didFinishUpdate

  • Add the 'inactive' flag 'to node userData

  • Make node subclass SKSpriteNode and add property inactive

  • Etc etc.
+3


source







All Articles