有时在我的SpriteKit程序中,我的碰撞和接触(使用SKPhysicsBody)没有按预期触发或起作用。我想我已经设置了所需的一切,但仍无法获得正确的交互。我如何轻松检查将与建立碰撞的物体和物体建立碰撞?
为了帮助诊断这些类型的问题,我编写了一个可以在任何地方调用的函数,该函数将分析当前场景并生成一个列表,列出哪些节点与其他节点发生碰撞以及将通知我的场景发生哪些碰撞。
该功能是独立的,不需要告知有关场景中节点的任何信息。
功能如下:
//MARK: - Analyse the collision/contact set up. func checkPhysics() { // Create an array of all the nodes with physicsBodies var physicsNodes = [SKNode]() //Get all physics bodies enumerateChildNodesWithName("//.") { node, _ in if let _ = node.physicsBody { physicsNodes.append(node) } else { print("\(node.name) does not have a physics body so cannot collide or be involved in contacts.") } } //For each node, check it's category against every other node's collion and contctTest bit mask for node in physicsNodes { let category = node.physicsBody!.categoryBitMask // Identify the node by its category if the name is blank let name = node.name != nil ? node.name : "Category \(category)" let collisionMask = node.physicsBody!.collisionBitMask let contactMask = node.physicsBody!.contactTestBitMask // If all bits of the collisonmask set, just say it collides with everything. if collisionMask == UInt32.max { print("\(name) collides with everything") } for otherNode in physicsNodes { if (node != otherNode) && (node.physicsBody?.dynamic == true) { let otherCategory = otherNode.physicsBody!.categoryBitMask // Identify the node by its category if the name is blank let otherName = otherNode.name != nil ? otherNode.name : "Category \(otherCategory)" // If the collisonmask and category match, they will collide if ((collisionMask & otherCategory) != 0) && (collisionMask != UInt32.max) { print("\(name) collides with \(otherName)") } // If the contactMAsk and category match, they will contact if (contactMask & otherCategory) != 0 {print("\(name) notifies when contacting \(otherName)")} } } } }
您还需要检查以下三件事:
SKPhysicsContactDelegate
physicsWorld.contactDelegate = self
didBeginContact
didEndcontact
设置完所有图片后应调用该函数-通常在didMoveToView工作结束时:
didMoveToView
checkPhysics()
didMoveToView在我的练习Swift项目的结尾处调用此函数时,将得到以下输出:
Optional(“ shape_blueSquare”)与Optional(“ Category 2147483648”)碰撞Optional(“ shape_blueSquare”)与Optional(“ shape_redCircle”)碰撞Optional(“ shape_blueSquare”)与Optional(“ shape_purpleSquare”)碰撞Optional(“ shape_blueSides)与Optional(“ shape_yellowTriangle”)Optional(“ shape_redCircle”)碰撞Optional(“ Category 2147483648”)Optional(“ shape_redCircle”)与Optional(“ shape_blueSquare”)碰撞Optional(“ shape_redCircle”)在与Optional(“ shape_purpleSquare”联系时通知)Optional(“ shape_redCircle”)与Optional(“ shape_yellowTriangle”)发生冲突。Optional(“ shape_redCircle”)在与Optional(“shape_yellowTriangle“)Optional(” shape_purpleSquare“)与Optional(” Category 2147483648“)Optional(” shape_purpleSquare“)碰撞与Optional(” shape_yellowTriangle“)Optional(” shape_yellowTriangle“)碰撞与所有didBere_replace发生冲突的对象()和Optional(“ shape_redCircle”)didBeginContact输入了Optional(“ shape_purpleSquare”)和Optional(“ shape_redCircle”)didBeginContact输入了Optional(“ shape_yellowTriangle”)和Optional(“ shape_redCircle”)shape_yellowTriangle“)Optional(” shape_yellowTriangle“)与为Optional(” shape_purpleSquare“)输入的didBeginContact和为Optional(” shape_purpleSquare“)输入的Optional(” shape_redCircle“)和为Optional(” shape_purpleSquare“)输入的Optional(” shape_redCircle“)didBeginContact的所有碰撞shape_yellowTriangle“)和Optional(” shape_redCircle“)shape_yellowTriangle“)Optional(” shape_yellowTriangle“)与为Optional(” shape_purpleSquare“)输入的didBeginContact和为Optional(” shape_purpleSquare“)输入的Optional(” shape_redCircle“)和为Optional(” shape_purpleSquare“)输入的Optional(” shape_redCircle“)didBeginContact的所有碰撞shape_yellowTriangle“)和Optional(” shape_redCircle“)))
类别2147483648是我的边界,并且没有名称。我给了这个类别以匹配它的crashBitMask
请随时尝试使用此功能,让我知道它是否有用或在任何情况下均无法解决。