在 Swift Xcode 中设置碰撞



我正在快速设置碰撞,当我遇到这个我似乎无法摆脱的错误时。我可以更改一行代码来修复该错误,但是当我这样做时,会发生更多错误。

 let collisionHitBox = CGRect(x: 201, y: 139, width: 398, height: 212)
 barCollisions.physicsBody? = SKPhysicsBody(edgeLoopFromRect: collisionHitBox)
    let ballCategory: UInt32 = 0x1 << 0
    let barCategory: UInt32 = 0x1 << 1
   ball.physicsBody?.categoryBitMask = ballCategory
    ball.physicsBody?.usesPreciseCollisionDetection = true
    ball.physicsBody?.collisionBitMask = ballCategory | barCategory
    ball.physicsBody?.contactTestBitMask = ballCategory | barCategory
     barCollisions.physicsBody?.categoryBitMask = barCategory
    barCollisions.physicsBody?.usesPreciseCollisionDetection = true



    func didBeginContact(contact: SKPhysicsContact) {
       let firstNode = contact.bodyA.node as! SKSpriteNode
       // This is where I get an error saying "Initialization of immutable value was never used, consider replacing it for removing it"    
        let secondNode = contact.bodyB.node as! SKSpriteNode
        if (contact.bodyA.categoryBitMask == ballCategory) && (contact.bodyB.categoryBitMask == barCategory)
        {
          let contactPoint = contact.contactPoint
            let contact_y = contactPoint.y
            let target_y = secondNode.position.y
            let margin = secondNode.frame.size.height/2 - 25
            if (contact_y > (target_y - margin)) && contact_y < (target_y + margin) {
                print("GameOver")

            }
        }

它只是说你设置了firstNode,但你没有使用它,所以你可以删除这一行

最新更新