简体   繁体   中英

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

Log: Here

Please excuse me if this is a stupid, horrid question, but I have been stuck on it and can't find answers anywhere. I have a few nodes: leftMaze, rightMaze, and player. I am trying to detect when the player collides with any of the other two nodes.

Keep in mind that the only physics I want to actually apply to the two maze nodes is gravity. Other than that, I just want them to pass through the player. Also, feel free to give me all the constructive criticism on my code as I am very new and want to get better! Thank you all so much!

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)

您的contactTestBitMask上没有contactTestBitMask ,因此从不调用didBegin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM