2017-03-14 4 views
-3

다른 SKSpritenode와의 접촉을 감지하고자하는 9 개의 SKSpritenodes가 있습니다. 연락처를 만들기 위해 ContactTestBitMask를 설정했지만 시작된 컨택의 코드는 호출되지 않았습니다. SKSpritenodes가 연락처를 생성하지 않음

접촉을 감지하지 못하고 있음을 내 SKSpritenodes 내 물리학 옵션 뭐가 문제 내 프로젝트

struct PhysicsCategory{ 

static let Circle : UInt32 = 0x1 << 0 
static let square1 : UInt32 = 0x1 << 1 
static let square2 : UInt32 = 0x1 << 2 
static let square3 : UInt32 = 0x1 << 3 
static let square4 : UInt32 = 0x1 << 4 
static let square5 : UInt32 = 0x1 << 5 
static let square6 : UInt32 = 0x1 << 6 
static let square7 : UInt32 = 0x1 << 7 
static let square8 : UInt32 = 0x1 << 8 
static let square9 : UInt32 = 0x1 << 9 

} 

    func createScene(){ 
    self.physicsWorld.contactDelegate = self 
    self.anchorPoint = CGPoint(x: 0, y: 0) 
    physics.append(PhysicsCategory.square1) 
    physics.append(PhysicsCategory.square2) 
    physics.append(PhysicsCategory.square3) 
    physics.append(PhysicsCategory.square4) 
    physics.append(PhysicsCategory.square5) 
    physics.append(PhysicsCategory.square6) 
    physics.append(PhysicsCategory.square7) 
    physics.append(PhysicsCategory.square8) 
    physics.append(PhysicsCategory.square9) 

    createSquares() 
    createCircles() 


} 



func didBegin(_ contact: SKPhysicsContact) { 

    let firstBody = contact.bodyA 
    let secondBody = contact.bodyB 

    for i in 0...8{ 

     if firstBody.categoryBitMask == PhysicsCategory.Circle && secondBody.categoryBitMask == physics[i] || firstBody.categoryBitMask == physics[i] && secondBody.categoryBitMask == PhysicsCategory.Circle { 

      changeColor(i:i) 
      circle.color = .red 
      circle.colorBlendFactor = 1.0 

     } 

    } 


     } 


    squares = (0...8).map { _ in square() } 

    for i in (0...8){ 


     squares[i].node.position = positions[i] 
     squares[i].node.physicsBody?.categoryBitMask = physics[i] 
     squares[i].node.physicsBody = SKPhysicsBody(rectangleOf: squares[i].node.size) 
     squares[i].node.physicsBody?.affectedByGravity = false 
     squares[i].node.physicsBody?.isDynamic = false 
     squares[i].node.physicsBody?.contactTestBitMask = PhysicsCategory.Circle 
     squares[i].node.physicsBody?.collisionBitMask = 0 
     squares[i].node.color = squares[i].startColor 
     squares[i].node.colorBlendFactor = 1.0 
     squares[i].node.size = CGSize(width: squareUnit, height: squareUnit) 



    } 


    squares.forEach { self.addChild($0.node) } 

} 

의 물리학 부분이다. 여기에 발생 physicsBody 초기화 후

squares[i].node.physicsBody?.categoryBitMask = physics[i] 

: 당신의 문제에 대한

+4

관련 코드 만 게시하십시오. 어떤 디버깅을 했습니까? – rmaddy

+1

적어도 하나의 몸은 역동적이어야합니다 ... 당신의 사각형과 원은 모두 정적입니다. – Whirlwind

+0

하지만 충돌을 테스트하지는 않습니다. – Repardeimaj

답변

2

솔루션 (createSquares() 방법)이 줄을 이동하는 것입니다

squares[i].node.physicsBody = SKPhysicsBody(rectangleOf: squares[i].node.size) 

그리고이 같은 연락처를 처리 할 수 ​​:

func didBegin(_ contact: SKPhysicsContact) { 

    let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask 

    switch contactMask { 
    case PhysicsCategory.Circle | PhysicsCategory.square1: 


     print("square 1") 
    case PhysicsCategory.Circle | PhysicsCategory.square2: 


     print("square 2") 
    case PhysicsCategory.Circle | PhysicsCategory.square3: 


     print("square 3") 
    case PhysicsCategory.Circle | PhysicsCategory.square4: 


     print("square 4") 


    // and so on ... 

    default : 
     //Some other contact has occurred 
     print("Some other contact") 
    } 


} 
관련 문제