简体   繁体   English

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

[英]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. 我有几个节点:leftMaze,rightMaze和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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