iOS SpriteKit - 碰撞和接触未按预期工作

iOS SpriteKit - collisions and contacts not working as expected

有时在我的 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)")}
                }
            }
        }  
    }

您还需要检查这 3 件事:

  1. 场景必须SKPhysicsContactDelegate
  2. 您必须设置physicsWorld.contactDelegate = self
  3. 您需要实现可选方法之一 SKPhysicsContactDelegate:

didBeginContact

didEndcontact

设置完所有图片后应调用该函数 - 通常在 didMoveToView 作品结束时调用:

checkPhysics()

当我在练习 Swift 项目中从 didMoveToView 的末尾调用此函数时,我得到以下输出:

Optional("shape_blueSquare") collides with Optional("Category 2147483648") Optional("shape_blueSquare") collides with Optional("shape_redCircle") Optional("shape_blueSquare") collides with Optional("shape_purpleSquare") Optional("shape_blueSquare") collides with Optional("shape_yellowTriangle") Optional("shape_redCircle") collides with Optional("Category 2147483648") Optional("shape_redCircle") collides with Optional("shape_blueSquare") Optional("shape_redCircle") notifies when contacting Optional("shape_purpleSquare") Optional("shape_redCircle") collides with Optional("shape_yellowTriangle") Optional("shape_redCircle") notifies when contacting Optional("shape_yellowTriangle") Optional("shape_purpleSquare") collides with Optional("Category 2147483648") Optional("shape_purpleSquare") collides with Optional("shape_yellowTriangle") Optional("shape_yellowTriangle") collides with everything didBeginContact entered for Optional("shape_purpleSquare") and Optional("shape_redCircle") didBeginContact entered for Optional("shape_purpleSquare") and Optional("shape_redCircle") didBeginContact entered for Optional("shape_yellowTriangle") and Optional("shape_redCircle")

类别 2147483648 是我的边缘边界,它没有名称。我给它这个类别来匹配它的 collisionBitMask

请随时尝试此功能,如果它有用或有任何它无法处理的情况,请告诉我。