2016-04-13 21 views
0

Dies ist mein erster Versuch mit SpriteKit und ich habe Probleme, meine Kollision mit Bitmasken richtig zu machen. Ich habe drei Kategorien, Wenn Spieler Treffer leuchtet, möchte ich die Punktzahl erhöhen und den beleuchteten Knoten vom Bildschirm verschieben, sonst möchte ich meine gameover() -Funktion aufrufen. Ich habe viele Variationen ausprobiert und kann nichts sehen, außer dass eine allgemeine Kollision erkannt wird. Ich habe auch die Kategorie- und Kontakt-Bitmasken für jeden Knoten definiert.Swift Spritekit Collision Handling

let playerCategory: UInt32 = 1 
let razzCategory: UInt32 = 2 
let litCategory: UInt32 = 4 

func didBeginContact(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 & playerCategory) == 0 && (secondBody.categoryBitMask & litCategory) == 1) 
    { 
     lit.position.x = 400 
     score += 1 
    } 
    else { 
     gameOver() 
    } 

} 
+0

Sie nicht wollen, die, wenn die Bedingung 0 und 1 gleich, Sie wollen, dass sie die Kategorie gleich Sie – Knight0fDragon

+0

id didBeginContact überhaupt genannt werden überprüft? – hamobi

Antwort

0

if ((firstBody.categoryBitMask & playerCategory) == 0 && (secondBody.categoryBitMask & litCategory) == 1) übersetzt die in Englisch folgenden.

Wenn Firstbody UND playerCategory = 0 und SecondBody Und litCategory = 1

Wenn Firstbody AND 1 = 0 und SecondBody und 4 = 1

Lassen Sie uns nun Firstbody als playerCategory und SecondBody als litCategory

definieren wenn playerCategory uND playerCategory = 0 und litCategory und litCategory = 1

wenn 1 AND 1 = 0 und 4 und 4 = 1

Wenn 1 = 0 UND 4 = 1

Wie Sie sehen, schlägt dies fehl, und diese Methode wird immer fehlschlagen, wenn die zweite Hälfte Ihres Tests (SecondBody UND litCategory) nur den Wert 0 haben kann oder 4, diese zwei Werte wird es nie sein 1.

das Problem zu beheben, wollen Sie sicherstellen, dass was auch immer Körper Sie überprüfen in die Kategorie gleich Sie suchen

if ((firstBody.categoryBitMask & playerCategory) == playerCategory && (secondBody.categoryBitMask & litCategory) == litCategory) 

Was sagt Ist der FirstBody Mitglied der Kategorie playerCategory und der secondBody ist ein Mitglied von litCategory, führen Sie den folgenden Vorgang aus iion.

Unten ist die komplette Lösung für Ihre Funktion:

func didBeginContact(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 & playerCategory) == playerCategory && (secondBody.categoryBitMask & litCategory) == litCategory) 
    { 
     lit.position.x = 400 
     score += 1 
    } 
    else { 
     gameOver() 
    } 

} 
+0

Es funktioniert! Vielen Dank für das Erklären und Helfen! – user69309