节点发生碰撞但未响应 didBeginContact 函数

Nodes are colliding but not responding to didBeginContact function

当我 运行 它时,我的节点正在碰撞。玩家节点的硬币反弹。但是,当我想调用 didBeginContact 函数时它没有响应...... 我最终想要一个标签,每次硬币击中玩家时都显示分数 +1。当与玩家碰撞时,硬币也应该消失。但是我的联系人不起作用,所以我无法制定任何碰撞规则使标签显示分数。

import SpriteKit
import GameplayKit

// Collision categories

struct physicsCategory {
static let playerCat : UInt32 = 1
static let coinCat : UInt32 = 2
}

class GameScene: SKScene, controls, SKPhysicsContactDelegate {

let player  = SKSpriteNode(imageNamed:"trump")
let points = SKLabelNode()
let buttonDirLeft = SKSpriteNode(imageNamed: "left")
let buttonDirRight = SKSpriteNode(imageNamed: "right")
let background = SKSpriteNode(imageNamed: "background")
var pressedButtons = [SKSpriteNode]()
let popUpMenu = SKSpriteNode(imageNamed: "popupmenu")
var score = 0
var gameOver = false
var startGame = false
var rules = false

override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self

//score label
points.position = CGPoint(x: 530, y: 260)
points.text =  ("\(score)")
points.zPosition = 6
points.fontColor = UIColor.black
points.fontSize = 50
addChild(points)

//Set Background
background.zPosition = 1
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height /  2)
background.size.width = 580
background.size.height = 320

addChild(background)

// Player
player.position = CGPoint(x: 250, y: 40)
player.zPosition = 2
player.size.width = 40
player.size.height = 60
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
player.physicsBody?.affectedByGravity = false
player.physicsBody!.categoryBitMask = physicsCategory.playerCat
player.physicsBody!.contactTestBitMask = physicsCategory.coinCat
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.isDynamic = false
self.addChild(player)

//contact has started
func didBeginContact(contact: SKPhysicsContact){
    let firstBody: SKPhysicsBody = contact.bodyA
    let secondBody: SKPhysicsBody = contact.bodyB

    if ((firstBody.categoryBitMask == physicsCategory.playerCat) &&  (secondBody.categoryBitMask == physicsCategory.coinCat)){
        CollisionWithCoin(player: firstBody.node as! SKSpriteNode, coins: secondBody.node as! SKSpriteNode)
    }
   }

func CollisionWithCoin(player: SKSpriteNode, coins:SKSpriteNode){
    NSLog("Hello")
}


//repeat coing spawning
run(SKAction.repeatForever(
    SKAction.sequence([
        SKAction.run(spawnCoins),
        SKAction.wait(forDuration: 1.0)])))
}
//coin settings
func random() -> CGFloat {
    return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}

func random(min: CGFloat, max: CGFloat) -> CGFloat {
    return random() * (max - min) + min
}

//spawn coins
func spawnCoins() {
    // 2
    let coins = SKSpriteNode(imageNamed: "coins")
    coins.zPosition = 2
    coins.size.width = 25
    coins.size.height = 25
    coins.physicsBody = SKPhysicsBody(rectangleOf: coins.size )
    coins.physicsBody!.categoryBitMask = physicsCategory.coinCat
    coins.physicsBody!.contactTestBitMask = physicsCategory.playerCat
    coins.physicsBody?.collisionBitMask = 1
    coins.position = CGPoint(x: frame.size.width * random(min: 0, max: 1), y: frame.size.height + coins.size.height/2)

    let action = SKAction.moveTo(y: -350, duration: TimeInterval(random(min: 1, max: 5)))

    let remove = SKAction.run({coins.removeFromParent()})

    let sequence = SKAction.sequence([action,remove])


    coins.run(sequence)

    addChild(coins)

}

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    /* Called before each frame is rendered */

    if pressedButtons.index(of: buttonDirLeft) != nil {
        player.position.x -= 4.0
    }
    if pressedButtons.index(of: buttonDirRight) != nil {
        player.position.x += 4.0
    }

}
//MOVEMENT FUNCTIONS START HERE
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

        for button in [buttonDirLeft, buttonDirRight] {
            // I check if they are already registered in the list
            if button.contains(location) && pressedButtons.index(of: button) == nil {
                pressedButtons.append(button)

                }
            }
        }
    }


override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

        for button in [buttonDirLeft, buttonDirRight] {
            // if I get off the button where my finger was before
            if button.contains(previousLocation)
                && !button.contains(location) {
                // I remove it from the list
                let index = pressedButtons.index(of: button)
                if index != nil {
                    pressedButtons.remove(at: index!)
                }
            }

                // if I get on the button where I wasn't previously
            else if !button.contains(previousLocation)
                && button.contains(location)
                && pressedButtons.index(of: button) == nil {
                // I add it to the list
                pressedButtons.append(button)

            }}}}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

        for button in [buttonDirLeft, buttonDirRight] {
            if button.contains(location) {
                let index = pressedButtons.index(of: button)
                if index != nil {
                    pressedButtons.remove(at: index!)
                }
            }
            else if (button.contains(previousLocation)) {
                let index = pressedButtons.index(of: button)
                if index != nil {
                    pressedButtons.remove(at: index!)
                }
            }
        }
    }
}


override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch: AnyObject in touches {
        let location = touch.location(in: self)
        let previousLocation = touch.previousLocation(in: self)

    for button in [buttonDirLeft, buttonDirRight] {
        if button.contains(location) {
            let index = pressedButtons.index(of: button)
            if index != nil {
                pressedButtons.remove(at: index!)
            }
        }
        else if (button.contains(previousLocation)) {
            let index = pressedButtons.index(of: button)
            if index != nil {
                pressedButtons.remove(at: index!)
            }
        }
    }
}
}

}

你能尝试像这样定义你的位掩码吗?

  enum PhysicsCategory {
      static let playerCat: UInt32 =  0x1 << 0
      static let coinCat: UInt32 =  0x1 << 1
}

你可以在你的联系方式中试试这个代码吗?另请注意,如果您使用的是 Swift3 联系方式名称的名称已更改。

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


        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        } else {
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }

        if ((firstBody.categoryBitMask == physicsCategory.playerCat) &&    (secondBody.categoryBitMask == physicsCategory.coinCat)){
            CollisionWithCoin(player: firstBody.node as! SKSpriteNode, coins: secondBody.node as! SKSpriteNode)
    }
   }
}

你也在用几个!在您的代码中,这会降低安全性。尝试使用 ?和 "if let" 在处理可选项时尽可能。因此,例如,即使您知道自己刚刚创建了它,也可以像这样编写您的物理体。您有时会这样做,而有时您会使用!,请保持一致。

  player.physicsBody?.categoryBitMask...
  etc

如果那个物理体出于某种原因 is/becomes nil 而你正在使用 !你会崩溃的。

我也会这样写你的联系方法,以确保如果联系方法因同一碰撞多次触发,你也不会崩溃。

  func collisionWithCoin(player: SKSpriteNode?, coins:SKSpriteNode?){
         guard let player = player, let coins = coins else { return }

         print("Hello")
  } 

然后在 didBeginContact 方法中这样调用它

 collisionWithCoin(player: firstBody.node as? SKSpriteNode, coins: secondBody.node as? SKSpriteNode)

最后我也会尝试遵循 swift 指南,你的方法应该以小写字母开头,类,结构应该以大写字母开头。

希望对您有所帮助