Ste*_*ves 4 collision-detection sprite-kit skphysicsbody swift
有时在我的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)")}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还需要检查以下三件事:
SKPhysicsContactDelegatephysicsWorld.contactDelegate = selfSKPhysicsContactDelegate:didBeginContact
didEndcontact
设置完所有图片后应调用该函数-通常在didMoveToView工作结束时:
checkPhysics()
Run Code Online (Sandbox Code Playgroud)
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碰撞”“和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“)和Optional(” shape_redCircle“)didBeginContact输入Optional(” shape_purpleSquare“)和Optional(” shape_redCircle“)didBeginContact输入的所有内容碰撞shape_yellowTriangle“)和Optional(” shape_redCircle“)shape_yellowTriangle“)Optional(” shape_yellowTriangle“)与为Optional(” shape_purpleSquare“)和Optional(” shape_redCircle“)didBeginContact输入Optional(” shape_purpleSquare“)和Optional(” shape_redCircle“)didBeginContact输入的所有内容碰撞shape_yellowTriangle“)和Optional(” shape_redCircle“)))
类别2147483648是我的边界,并且没有名称。我给了这个类别以匹配它的crashBitMask
请随时尝试使用此功能,让我知道它是否有用或是否有任何无法解决的情况。
| 归档时间: |
|
| 查看次数: |
1046 次 |
| 最近记录: |