2017-02-04 20 views
0

私はすべてが衝突をうまく検出している4つの物理機関を持っています。しかし、衝突すると検出されない2つの物理機関が存在します。彼らは他の物理機関と衝突したときに検出します。私はそれらのすべてのためにcontacttestbitmasksを持っているので、なぜ問題があるのか​​分からない。ここではいくつかのコードは次のとおりです。ここで ここで私のセットアップ私の物理学機関:spritekitの物理衝突が検出されない

 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 
:ここ
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 
} 

は、私は問題の下剤団体の一つであるプレイヤーの物理体を設定するために使用されるコードでありますここ

は、衝突を検出するためのFUNCです:

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) 
    } 

} 

ここに私がblueballを設定するために使用されるコードです。これは、問題を抱えている他の物理学者の身体です:

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) 

ここでのアイデアは参考になります。私は運がないまま数日間問題を見つけようとしています。

+0

コメントあなたのコード行をして、もう一度それを試してみてください 'player.physicsBody .isDynamic = true'に – Mina

+0

@Minaはそれをやりました。何も変わらない。 –

+0

両方のノード、プレーヤーとblueballのためのコメント、私は問題がisDynamicプロパティのためだと思う。 – Mina

答えて

1

複数のカテゴリを設定する場合は、またはの値を一緒に設定する必要があります。あなたのコード

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

カテゴリを2回設定するだけで、2番目のカテゴリは最初のものを上書きします。これを変更します?

player.physicsBody?.contactTestBitMask = PhysicsCategory.blueball | PhysicsCategory.redball 
関連する問題