无法将 PhysicsBody 和 属性 分配给 SKSpriteNode?

Cannot assign PhysicsBody and property to an SKSpriteNode?

我正在构建一个函数,其中随机变量将选择数组中 4 个 SKSprite 节点中的 1 个并将其自身分配给它。然而,该随机线虽然在屏幕上按预期显示,但不包含任何物理体 属性 因此它不会与其他节点发生碰撞。下面是我的代码:

func line() {

        redLine = SKSpriteNode(imageNamed: "redline")
        blueLine = SKSpriteNode(imageNamed: "blueline")
        yellowLine = SKSpriteNode(imageNamed: "yellowline")
        greenLine = SKSpriteNode(imageNamed: "greenline")

        let lineArray = [redLine,blueLine,yellowLine,greenLine]
        // Add physics
        lineArray[0].physicsBody?.categoryBitMask = gamePhysics.RedLine
        lineArray[1].physicsBody?.categoryBitMask = gamePhysics.BlueLine
        lineArray[2].physicsBody?.categoryBitMask = gamePhysics.YellowLine
        lineArray[3].physicsBody?.categoryBitMask = gamePhysics.GreenLine

        for i in 0...3 {
            lineArray[i].physicsBody = SKPhysicsBody(circleOfRadius: 10)
            lineArray[i].physicsBody?.collisionBitMask = gamePhysics.RedBall | gamePhysics.BlueBall | gamePhysics.YellowBall | gamePhysics.GreenBall
            lineArray[i].physicsBody?.contactTestBitMask = gamePhysics.RedBall | gamePhysics.BlueBall | gamePhysics.YellowBall | gamePhysics.GreenBall
            lineArray[i].physicsBody?.affectedByGravity = false
            lineArray[i].physicsBody?.isDynamic = true
        }
        let randomLine:SKSpriteNode! = lineArray.randomElement()
        ...

        self.addChild(randomLine)
        ...

        randomLine.run(SKAction.sequence([moveLine,delay,removeLine]))
    }

所以基本上当随机线与其他节点碰撞时,什么也不会发生。

这是我的 didBegin(联系人)函数:

func didBegin(_ contact: SKPhysicsContact) {
        let firstBody = contact.bodyA
        let secondBody = contact.bodyB

        if firstBody.categoryBitMask == gamePhysics.RedBall && secondBody.categoryBitMask == gamePhysics.RedLine || firstBody.categoryBitMask == gamePhysics.BlueBall && secondBody.categoryBitMask == gamePhysics.BlueLine || firstBody.categoryBitMask == gamePhysics.YellowBall && secondBody.categoryBitMask == gamePhysics.YellowLine || firstBody.categoryBitMask == gamePhysics.GreenBall && secondBody.categoryBitMask == gamePhysics.GreenLine {

            currentScore += 1
            secondBody.node?.removeFromParent()
            print("hit point")

        } else if firstBody.categoryBitMask == gamePhysics.RedLine && secondBody.categoryBitMask == gamePhysics.RedBall || firstBody.categoryBitMask == gamePhysics.BlueLine && secondBody.categoryBitMask == gamePhysics.BlueBall || firstBody.categoryBitMask == gamePhysics.YellowLine && secondBody.categoryBitMask == gamePhysics.YellowBall || firstBody.categoryBitMask == gamePhysics.GreenLine && secondBody.categoryBitMask == gamePhysics.GreenBall {

            currentScore += 1
            firstBody.node?.removeFromParent()
            print("hit point")

        }
    }

如有任何帮助,我们将不胜感激! 编辑:这是球 func()。也许 SKNode 以某种方式包含多个类别位掩码,因此无法正常工作?

func Ball() {

        ballNode = SKNode()
        redBall = SKSpriteNode(imageNamed: "redball")
        blueBall = SKSpriteNode(imageNamed: "blueball")
        yellowBall = SKSpriteNode(imageNamed: "yellowball")
        greenBall = SKSpriteNode(imageNamed: "greenball")

        let ballArray = [redBall,blueBall,yellowBall,greenBall]
        ...

        // Add physics
        ballArray[0].physicsBody?.categoryBitMask = gamePhysics.RedBall
        ballArray[1].physicsBody?.categoryBitMask = gamePhysics.BlueBall
        ballArray[2].physicsBody?.categoryBitMask = gamePhysics.YellowBall
        ballArray[3].physicsBody?.categoryBitMask = gamePhysics.GreenBall

        for i in 0...3 {
            ballArray[i].physicsBody = SKPhysicsBody(circleOfRadius: 20)
            ballArray[i].physicsBody?.collisionBitMask = gamePhysics.RedLine | gamePhysics.BlueLine | gamePhysics.YellowLine | gamePhysics.GreenLine
            ballArray[i].physicsBody?.contactTestBitMask = gamePhysics.RedLine | gamePhysics.BlueLine | gamePhysics.YellowLine | gamePhysics.GreenLine
            ballArray[i].physicsBody?.affectedByGravity = false
            ballArray[i].physicsBody?.isDynamic = false
        }

        ballNode.addChild(redBall)
        ballNode.addChild(greenBall)
        ballNode.addChild(yellowBall)
        ballNode.addChild(blueBall)

        self.addChild(ballNode)
    }

更新 2: 这是我存储类别位掩码的结构:

struct gamePhysics {

    static let RedBall : UInt32 = 0x1 << 1
    static let BlueBall : UInt32 = 0x1 << 2
    static let GreenBall : UInt32 = 0x1 << 3
    static let YellowBall : UInt32 = 0x1 << 4
    static let RedLine : UInt32 = 0x1 << 5
    static let BlueLine : UInt32 = 0x1 << 6
    static let GreenLine : UInt32 = 0x1 << 7
    static let YellowLine : UInt32 = 0x1 << 8
}

您还记得在场景中设置接触委托吗?将这行代码放在 didMove(to:):

self.physicsWorld.contactDelegate = self

如果这不能解决问题,我会在 &&|| 语句之间使用括号使 didBegin 中的 if 条件更加明确。

查看其余代码,我看不出还有什么问题,您所做的应该有效。你放在 if 语句中的 print("hit point") 是否达到了?

天哪,我找到了问题的答案。 categorybitmask 的值为 return "nil" 的原因是因为我实际上在指定其 physicsbody 之前将节点分配给 categorybitmask。把线往下移几行,一切都解决了!