2017-02-04 5 views
0

Ich habe 4 Physikkörper, die alle Kollisionen gut erkennen. Es gibt jedoch zwei Physikkörper, die nicht erkennen, wenn sie miteinander kollidieren. Sie entdecken jedoch, wenn sie mit anderen Physikkörpern kollidieren. Ich habe kontakttestbitmasks für alle sie, also verstehe ich nicht, warum es ein Problem gibt. Hier ist ein Code: Hier ist, wo ich mein Setup Physik Körper:Spritekit Physik Kollision nicht erkannt werden

struct PhysicsCategory{ 
    static let None  : UInt32 = 0 
    static let All  : UInt32 = UInt32.max 
    static let player : UInt32 = 0b1 
    static let bounce : UInt32 = 0b10 
    static let blueball : UInt32 = 0b100 
    static let redball : UInt32 = 0b1000 
    static let coin  : UInt32 = 0b10000 
} 

Hier ist der Code, wo ich verwendet für den Aufbau der Spieler Physik Körper, die eine der Problem Physic Körper ist:

 player.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: player.size.width-40, height: player.size.height-40)) 
    player.physicsBody?.isDynamic = true 
    player.physicsBody?.categoryBitMask = PhysicsCategory.player 
    player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball 
    player.physicsBody?.contactTestBitMask = PhysicsCategory.redball 
    player.physicsBody?.collisionBitMask = PhysicsCategory.None 

Hier ist die func zur Erkennung von Kollisionen:

func didBegin(_ contact: SKPhysicsContact) { 

    var firstBody: SKPhysicsBody 
    var 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 & PhysicsCategory.player != 0) && 
     (secondBody.categoryBitMask & PhysicsCategory.redball != 0)) { 
     RedballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, redball: secondBody.node as! SKSpriteNode) 
    } 
    if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) && 
     (secondBody.categoryBitMask & PhysicsCategory.blueball != 0)) { 
     BlueballDidCollideWithPlayer(player: firstBody.node as! SKSpriteNode, blueball: secondBody.node as! SKSpriteNode) 
    } 
    if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) && 
     (secondBody.categoryBitMask & PhysicsCategory.redball != 0)){ 
     RedballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, redball: secondBody.node as! SKSpriteNode) 
    } 
    if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) && 
     (secondBody.categoryBitMask & PhysicsCategory.blueball != 0)) { 
     BlueballDidCollideWithBounce(bounce: firstBody.node as! SKSpriteNode, blueball: secondBody.node as! SKSpriteNode) 
    } 
    if ((firstBody.categoryBitMask & PhysicsCategory.bounce != 0) && 
     (secondBody.categoryBitMask & PhysicsCategory.coin != 0)) { 
     coinDidCollideWithplayer(player: firstBody.node as! SKSpriteNode, coin: secondBody.node as! SKSpriteNode) 
    } 
    if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) && 
     (secondBody.categoryBitMask & PhysicsCategory.coin != 0)) { 
     coinDidCollideWithplayer(player: firstBody.node as!  SKSpriteNode, coin: secondBody.node as! SKSpriteNode) 
    } 

} 

Hier ist der Code, den ich die blueball für die Einrichtung verwendet. Dies ist der andere physikalische Körper, der Probleme hat:

let blueball = SKSpriteNode(imageNamed: "blueball") 
     blueball.position = enemyb.position 
     blueball.physicsBody = SKPhysicsBody(circleOfRadius: blueball.size.width/2) 
     blueball.physicsBody?.isDynamic = true 
     blueball.physicsBody?.categoryBitMask = PhysicsCategory.blueball 
     blueball.physicsBody?.contactTestBitMask = PhysicsCategory.player 
     blueball.physicsBody?.contactTestBitMask = PhysicsCategory.bounce 
     blueball.physicsBody?.collisionBitMask = PhysicsCategory.None 
     blueball.physicsBody?.usesPreciseCollisionDetection = true 
     addChild(blueball) 
     let actionMove = SKAction.move(to: player.position, duration: 2) 

Alle Ideen hier wären hilfreich. Ich habe versucht, das Problem für ein paar Tage ohne Glück zu finden.

+0

Kommentar diese Zeile des Codes und versuchen Sie es erneut, 'player.physicsBody .isDynamic = true' – Mina

+0

@Mina es tat. Ändert nichts. –

+0

kommentieren Sie es für beide Knoten, Spieler und Blueball, ich denke, das Problem liegt an isDynamic Eigenschaft. – Mina

Antwort

1

Wenn Sie mehrere Kategorien einstellen, müssen Sie OR die Werte zusammen. Ihr Code

player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball 
player.physicsBody?.contactTestBitMask = PhysicsCategory.redball 

Setzt die Kategorie zweimal, die zweite überschreibt die erste. Ändern Sie dies in:

player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball | PhysicsCategory.redball 
Verwandte Themen