Sprite-Kit registering multiple collisions for single contact

前端 未结 1 1404
逝去的感伤
逝去的感伤 2020-12-03 19:19

OK - I’m sure this is a duplicate and that Whirlwind or KnightOfDragons has posted a solution, but I can’t find it.

I’m writing a clone of Space Invader in Swift us

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 19:54

    OK - it would appear that a simple:

            if bomb == nil {return}
    

    is all that's required. This should be added as follows:

            let bomb = contact.bodyA.categoryBitMask == category.bomb.rawValue ? contact.bodyA.node : contact.bodyB.node
            if bomb == nil {return}
    

    This works to prevent multiple collisions for a node that you removeFromParent in didBeginContact. If you don;t remove the node but are still registering multiple collisions, then use the node's userData property to set some sort of flag indicating that the node i s'anactive' Alternately, subclass SKSPriteNode and add a custom 'isActive' property, which is what I did to solve my problem of bullets passing up the side of an invader and taking out that invader and the one above it. This never happens on a 'direct hit'.

    It doesn't answer the underlying question as to why SK is registering multiple collisions, but it does mean that I can remove all the extra code concerning setting contactTestBitMasks to 0 and then back to what they should be later etc.

    Edit: So it appears that if you delete a node in didBeginContact, the node is removed but the physics body isn't. So you have to be careful as Sprite-Kit appears to build an array of physicsContacts that have occurred and then calls dBC multiple times, once for each contact. So if you are manipulating nodes and also removing them in dBC, be aware that you might run into an issue if you force unwrap a node's properties.

    0 讨论(0)
提交回复
热议问题