0

日志:这里

如果这是一个愚蠢,可怕的问题,请原谅,但我一直坚持下去,无法在任何地方找到答案。我有几个节点: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)
4

1 回答 1

0

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

于 2017-07-26T03:04:20.613 回答