如何检测 Swift 中两个 SKShapeNode 的碰撞而不影响它们的物理特性?

How do I detect the collision of two SKShapeNodes in Swift without affecting their physics?

日志:Here

如果这是一个愚蠢、可怕的问题,请原谅,但我一直被困在这个问题上,无法在任何地方找到答案。我有几个节点:leftMaze、rightMaze 和 player。我正在尝试检测玩家何时与其他两个节点中的任何一个发生碰撞。

请记住,我想要实际应用于两个迷宫节点的唯一物理是重力。除此之外,我只想让他们通过播放器。另外,请随时给我所有对我的代码的建设性批评,因为我是新手并且想要变得更好!非常感谢大家!

override func didMove(to view: SKView) {

    backgroundColor = SKColor.white

    // PLAYER CONFIG //

    player.xScale = 0.25
    player.yScale = 0.25
    player.position = CGPoint(x:0,y:0 - (self.frame.height/4))
    player.physicsBody = SKPhysicsBody(circleOfRadius: max(player.size.width / 2,
                                                           player.size.height / 2))
    player.physicsBody!.affectedByGravity = false
    player.physicsBody!.collisionBitMask = 0
    // SPAWNING GAME OBJECTS //

    addChild(player)

    // DEBUG //

    print(size.width, " / " ,size.height)
    print(CGPoint(x: size.width * 0.5 ,y: size.height * 0.1))

    timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(createMaze),userInfo:nil, repeats: true)

}

func didBegin(_ contact: SKPhysicsContact) {
    if contact.bodyA.node == player && contact.bodyB.node == leftMaze{

       print("Left Collision!")
    }
}

func createMaze(){


    /*-------- Right Maze Init --------*/

    // HOW BIG THE RIGHT PART OF THE MAZE IS
    let randomNumber = arc4random_uniform(UInt32(self.frame.width * 0.66))
    // HOW FAR TO PLACE THE RIGHT PART OF THE MAZE FROM THE RIGHT PART OF THE SCREEN
    let distanceRight = self.frame.maxX-CGFloat(randomNumber)
    // DEFINITION OF RIGHT MAZE
    rightMaze.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: Int(randomNumber), height: mazeHeight), cornerRadius: 0).cgPath
    rightMaze.position = CGPoint(x: CGFloat(distanceRight), y: frame.maxY)
    rightMaze.fillColor = UIColor.black

    //ADDING A PHYSICS BODY AND GRAVITY
    rightMaze.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: Int(randomNumber), height: mazeHeight))
    rightMaze.physicsBody!.affectedByGravity = true
    rightMaze.physicsBody!.collisionBitMask = 0
    /*-------- End of Right Maze Init --------*/

    /*-------- Left Maze Init --------*/

    // WHERE TO PLACE THE LEFT PART OF THE MAZE
    let distanceLeft = self.frame.minX
    // DEFINITION OF THE LEFT MAZE
    leftMaze.path = UIBezierPath(roundedRect: CGRect(x: 0, y: 0, width: Int(self.frame.width-CGFloat(randomNumber)-(player.size.width+20)), height: mazeHeight), cornerRadius: 0).cgPath
    leftMaze.position = CGPoint(x: CGFloat(distanceLeft), y: frame.maxY)
    leftMaze.fillColor = UIColor.black

    //ADDING A PHYSICS BODY AND GRAVITY
    leftMaze.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: Int(self.frame.width-CGFloat(randomNumber)-(player.size.width+20)), height: mazeHeight))
    leftMaze.physicsBody!.affectedByGravity = true
    leftMaze.physicsBody!.collisionBitMask = 0
    /*-------- End of Left Maze Init --------*/

    addChild(rightMaze)
    addChild(leftMaze)

你的 sprite 上没有 contactTestBitMask,所以你的 didBegin 永远不会被调用。