问题描述
AppImage 我有一堵由 4 个不同颜色的矩形组成的墙,可以通过穿过墙壁,球的颜色必须与墙上矩形的颜色相匹配.球会穿过墙壁,会出现新的墙壁.但是,当我检测到这种碰撞时,我会得到多个碰撞读数.我已经通过打印死或活来测试了这一点,并且它打印了两次或多次.
AppImage I have a wall of 4 rectangles that are different colors, to pass through the wall the color of the ball has to match that of the rectangle on the wall. The ball will pass through the wall, and a new wall will appear. However, when I detect this collision I get multiple collision readings. I have tested this by printing dead or alive, and it prints both or more 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")
}
}
}
请帮忙!!!
推荐答案
是的 - 这发生了.处理它的方法(在某些情况下,您不能让 sprite-kit 不多次调用 didBegin
)是确保您的联系代码适应这一点,并且多次处理合同不会导致问题(例如多次添加分数,删除多个生命,尝试访问已删除的节点或物理体等).
Yep - this happens. The way to handle it (you can't get sprite-kit to NOT call didBegin
multiple times in some circumstances) is to make sure that your contact code accommodates this and that handling the contract multiple times does not cause a problem (such as adding to the score multiple times, removing multiple lives, trying to access a node or physicsBody that has been removed etc).
这里有一个讨论:Sprite-Kit 为单个联系人注册多个碰撞
您可以做的一些事情包括:
Some things you can do include:
- 如果您删除了一个被联系的节点,请在之前检查它是否为
nil
您将其删除(对于重复的联系人) - 将节点添加到集合中,然后删除集合中的所有节点
didFinishUpdate
- 在节点的
userData
中添加一个inactive"标志 - 使节点成为 SKSpriteNode 的子类并添加
inactive
属性 - 等等等等
- If you remove a node that is contacted, check for it being
nil
beforeyou remove it (for the duplicate contacts) - Add the node to a set and then remove all the nodes in the set in
didFinishUpdate
- Add an 'inactive' flag' to the node's
userData
- Make the node a subclass of SKSpriteNode and add an
inactive
property - Etc etc.
这篇关于Swift Spritekit 我检测到碰撞,但它读取了多次碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!