2017-07-26 91 views
0

日誌:Here如何檢測Swift中兩個SKShapeNodes的碰撞而不影響其物理?

請原諒,如果這是一個愚蠢的,可怕的問題,但我一直卡在它,無法找到答案的任何地方。我有幾個節點: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) 
+0

@matt我想要控制檯輸出「Left Collision!」如在didBegin函數中看到的,但沒有任何事情發生。 – Blar321

回答

0

你有你的精靈沒有contactTestBitMask,所以你didBegin永遠不會被調用。