0
我有3個可以相互碰撞的小精靈,並設置了contactTestBitMask
和categoryBitMask
。 我有兩個精靈使用didBeginContact
方法來查看它們是否發生了碰撞並且工作正常,但是現在我不確定如何設置它來檢查多個對象的聯繫人並根據哪兩個物體發生碰撞來運行不同的函數。在SpriteKit遊戲中使用didBeginContact的正確方法 - Swift
我PhysicsCategory
:
struct PhysicsCategory {
static let None: UInt32 = 0
static let All: UInt32 = UInt32.max
static let Bird: UInt32 = 0b1
static let Cloud: UInt32 = 0b10
static let Desk: UInt32 = 0b100
static let Star: UInt32 = 0b101
static let StarSpecial: UInt32 = 0b110
}
的didBeginContact
方法GameScene
:
func didBeginContact(contact: SKPhysicsContact) {
var BirdBody: SKPhysicsBody?
var DeskBody: SKPhysicsBody?
var SpecialStarBody: SKPhysicsBody?
//var SpecialStarBody: SKPhysicsBody
if contact.bodyA.categoryBitMask == PhysicsCategory.Bird {
BirdBody = contact.bodyA
DeskBody = contact.bodyB
} else if contact.bodyA.categoryBitMask == PhysicsCategory.Desk {
BirdBody = contact.bodyB
DeskBody = contact.bodyA
} else if contact.bodyA.categoryBitMask == PhysicsCategory.StarSpecial {
SpecialStarBody = contact.bodyA
}
if (DeskBody != nil) {
birdSprite.BirdDidCollideWithDesk(BirdBody?.node as! Bird, desk: DeskBody?.node as! Desk, scoreClass: scoreClass, scoreLabel: scoreLabel)
}
}
我想要做的是檢查是否鳥來了與一張桌子接觸,在這種情況下,它應該運行鳥和桌子的功能,否則我想知道鳥是否接觸過一個SpecialStar,在這種情況下我沒有寫過一個函數來做什麼,但後來我會調用另一個函數這個案例。我還添加了一些額外的精靈,所以我會很感激,如果任何人都可以解釋如何做這些代碼寫得很好,不會造成任何錯誤。
謝謝你,我需要的! – Larisa