同一个 SKPhysicsBody 多次调用 didBeginContact [英] didBeginContact is being called multiple times for the same SKPhysicsBody
问题描述
func didBeginContact(contact: SKPhysicsContact) {
if ( contact.bodyA.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue {
contact.bodyB.node?.removeFromParent()
counter++
println(counter)
} else if ( contact.bodyB.categoryBitMask & BodyType.shield.rawValue ) == BodyType.shield.rawValue {
contact.bodyA.node?.removeFromParent()
counter++
println(counter)
}
}
一个物理体来自纹理 shield.physicsBody = SKPhysicsBody(texture: shieldTexture, size: shieldTexture.size())
另一个来自一个圆 sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.size.width/2)
当两个物体有时相互接触时 sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.size.width/2)
被多次调用.我如何让它只为每个对象调用一次,即使我一接触它就从父对象中删除它.
When the tow objects contact each other sometimes sand.physicsBody = SKPhysicsBody(circleOfRadius: sand.size.width/2)
gets called multiple times. How do i get it to only get called once for each object even though i remove it from the parent as soon as it contacts.
推荐答案
当使用基于纹理的物理实体时,问题似乎更频繁地出现.如果在先前的碰撞触发器中指示相同的contact.bodyA.node",我解决它的方法是禁止didBegin(_contact:SKPhysicsContact)"继续.即:
The problem seems to occur more often when texture-based physics bodies are used. The way I got around it was to inhibit 'didBegin(_ contact: SKPhysicsContact)' from continuing if the same 'contact.bodyA.node' was indicated in the previous collision trigger. i.e.:
if lastNodeA != contact.bodyA.node {
lastNodeA = contact.bodyA.node
...
这篇关于同一个 SKPhysicsBody 多次调用 didBeginContact的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!