2014-10-04 51 views
1

我正在通過三個節點到[didBeginContact]。我在代碼下寫道。 但無法正常工作。當白色的矩形沒有擊中黑色或白色矩形, println(「黑色」)和println(「藍色」)的工作... whien白色矩形打黑色矩形,println(「藍色」)工作...spritekit didBeginContact三個對象沒有工作

基於您的聯繫處理
import SpriteKit 

class GameScene: SKScene, SKPhysicsContactDelegate { 

let blackCategory: UInt32 = 0x1 << 0 
let whiteCategory: UInt32 = 0x1 << 1 
let blueCategory: UInt32 = 0x1 << 2 

override func didMoveToView(view: SKView) { 

    self.physicsWorld.contactDelegate = self 

    self.size = view.bounds.size 
    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame) 
    self.physicsWorld.gravity = CGVectorMake(0.0, -3.0) 

    let blackSquare = SKSpriteNode(color: UIColor.blackColor(), size: CGSizeMake(50, 50)) 
    blackSquare.position = CGPoint(
     x: CGRectGetMidX(self.frame), 
     y: CGRectGetMidY(self.frame) 
    ) 
    blackSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50)) 
    blackSquare.physicsBody?.affectedByGravity = false 
    blackSquare.physicsBody?.dynamic = false 

    blackSquare.physicsBody?.categoryBitMask = blackCategory 
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory 

    let blueSquare = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50)) 
    blueSquare.position = CGPoint(
     x: CGRectGetMidX(self.frame) - 100, 
     y: CGRectGetMidY(self.frame) - 100 
    ) 
    blueSquare.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50)) 
    blueSquare.physicsBody?.affectedByGravity = false 
    blueSquare.physicsBody?.dynamic = false 

    blackSquare.physicsBody?.categoryBitMask = blueCategory 
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory 

    self.addChild(blackSquare) 
    self.addChild(blueSquare) 
} 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 


    for touch in touches { 

     let location = touch.locationInNode(self) 
     let whiteRectangle = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(50, 50)) 
     whiteRectangle.position = location 
     whiteRectangle.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(50, 50)) 


     whiteRectangle.physicsBody?.categoryBitMask = whiteCategory 
     whiteRectangle.physicsBody?.contactTestBitMask = blackCategory 
     whiteRectangle.physicsBody?.contactTestBitMask = blueCategory 

     self.addChild(whiteRectangle) 
    } 

} 

override func update(currentTime: CFTimeInterval) { 
} 


func didBeginContact(contact: SKPhysicsContact!) { 

    var firstBody, secondBody, thirdBody: SKPhysicsBody 


    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
     firstBody = contact.bodyA 
     secondBody = contact.bodyB 
     thirdBody = contact.bodyB 
    } else { 
     firstBody = contact.bodyB 
     secondBody = contact.bodyA 
     thirdBody = contact.bodyA 
    } 

    if firstBody.categoryBitMask & whiteCategory != 0 && 
     secondBody.categoryBitMask & blackCategory != 0 { 
      //secondBody.node?.removeFromParent() 
      println("black") 
    } 

    if firstBody.categoryBitMask & whiteCategory != 0 && 
     thirdBody.categoryBitMask & blueCategory != 0 { 
      //secondBody.node?.removeFromParent() 
      println("blue") 
    } 
} 
} 
+1

「不工作」不是問題描述 – LearnCocos2D 2014-10-04 14:45:12

回答

2

1),你應該設置的類別是

let whiteCategory: UInt32 = 0x1 << 0 
    let blackCategory: UInt32 = 0x1 << 1 
    let blueCategory: UInt32 = 0x1 << 2 

2)你是不正確設置blackSquare的physicsBody的位掩碼兩次

blackSquare.physicsBody?.categoryBitMask = blueCategory 
    blackSquare.physicsBody?.contactTestBitMask = whiteCategory 

上述應該是

blueSquare.physicsBody?.categoryBitMask = blueCategory 
    blueSquare.physicsBody?.contactTestBitMask = whiteCategory 

3)這裏,不需要第三接觸體。

func didBeginContact(contact: SKPhysicsContact!) { 

    var firstBody, secondBody: SKPhysicsBody 

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask { 
     firstBody = contact.bodyA 
     secondBody = contact.bodyB 
    } else { 
     firstBody = contact.bodyB 
     secondBody = contact.bodyA 
    } 

    if ((firstBody.categoryBitMask & whiteCategory) != 0 && 
     (secondBody.categoryBitMask & blackCategory != 0)) { 
      //secondBody.node?.removeFromParent() 
      println("black") 
    } 


    if ((firstBody.categoryBitMask & whiteCategory != 0) && 
     (secondBody.categoryBitMask & blueCategory != 0)) { 
      //secondBody.node?.removeFromParent() 
      println("blue") 
    } 
} 

4)以下是不需要的(在touchesBegan中)。設置藍色和黑色節點的聯繫位掩碼就足夠了。

whiteRectangle.physicsBody?.contactTestBitMask = blackCategory 
    whiteRectangle.physicsBody?.contactTestBitMask = blueCategory 
相關問題