Why are didBeginContact called multiple times?

前端 未结 6 1175
孤独总比滥情好
孤独总比滥情好 2020-11-27 22:18

In an iOS game that uses Sprite Kit along with the contact detection in Sprite Kit\'s build-in physics engine, I decrease the Hero\'s number lives by one each time he gets i

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 23:04

    I came across the same issue. In my case the didBeginContact() was called many times (I counted up to 5 times) for one contact of a bullet with the enemy. As the bullet is a simple circle format, I agree with @SFX that it cannot be a bug just in Texture-Bodies. The tests have shown that there was no call to update() between the didBeginContact() calls. So the solution is simple (Swift):

    var updatesCalled = 0
    ...
    internal update() {
      updatesCalled ++
    }
    ...
    internal func didBeginContact(contact: SKPhysicsContact) {
        NSLog("didBeginContact: (\(contact.contactPoint.x), \(contact.contactPoint.y)), \(updatesCalled)")
        if(updatesCalled == 0) {return} // No real change since last call
        updatesCalled = 0
        ... your code here ...
    }
    

    I tried didEndContact() but that was not called at all. I didn't investigate further into this.

    BTW: I just switched from Android, and I'm impressed by the easiness and stability of this System :-)

提交回复
热议问题