問題:只要玩家節點與硬幣節點接觸,遊戲就會在遊戲只有在玩家與邊界碰撞時纔會結束。當玩家節點與另一個節點發生衝突時,遊戲結束時,遊戲只應在玩家與邊界碰撞時結束
輸出結果應該是:玩家應該能夠接觸到硬幣節點並穿過硬幣節點,爲scoreLabel添加一個值。 當前的代碼:
struct ColliderType {
static let playerCategory: UInt32 = 0x1 << 0
static let boundary: UInt32 = 0x1 << 1
static let coinCategory: UInt32 = 0x1 << 2
static let firstBody: UInt32 = 0x1 << 3
static let secondBody: UInt32 = 0x1 << 4
}
var gameOver = false
var coinInt = 0
coin.physicsBody?.categoryBitMask = ColliderType.coinCategory
coin.physicsBody?.contactTestBitMask = ColliderType.playerCategory
coin.physicsBody?.collisionBitMask = 0
player.physicsBody?.categoryBitMask = ColliderType.playerCategory
player.physicsBody?.contactTestBitMask = ColliderType.boundary | ColliderType.coinCategory
player.physicsBody?.collisionBitMask = ColliderType.boundary
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 == ColliderType.playerCategory && secondBody.categoryBitMask == ColliderType.coinCategory {
self.coin.removeFromParent()
coinInt += 1
coinLabel.text = "\(coinInt)"
}
gameOver = true
self.speed = 0
timer.invalidate()
}
override func touchesBegan(touches: Set<UITouch> , withEvent event: UIEvent?) {
if gameOver == false {
self.player.physicsBody?.velocity = CGVectorMake(1, 3)
self.player.physicsBody?.applyImpulse(CGVectorMake(0, 12))
}
}
UPDATE:
boundary.contactTestBitMask = ColliderType.playerCategory
boundary.categoryBitMask = ColliderType.boundary
boundary.collisionBitMask = ColliderType.playerCategory
coin.physicsBody?.categoryBitMask = ColliderType.coinCategory
coin.physicsBody?.contactTestBitMask = ColliderType.playerCategory
coin.physicsBody?.collisionBitMask = 0
player.physicsBody?.categoryBitMask = ColliderType.playerCategory
player.physicsBody?.contactTestBitMask = ColliderType.coinCategory
player.physicsBody?.collisionBitMask = ColliderType.boundary
func didBeginContact(contact:SKPhysicsContact) {
let firstBody: SKPhysicsBody
let 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 == ColliderType.coinCategory || secondBody.categoryBitMask == ColliderType.coinCategory {
self.coin.removeFromParent()
self.coinInt += 1
self.coinLabel.text = "\(self.coinInt)"
}else if firstBody.categoryBitMask == ColliderType.boundary || secondBody.categoryBitMask == ColliderType.boundary {
gameOver = true
self.speed = 0
timer.invalidate()
}
}
關於你的編輯:你的播放器沒有檢查邊界,而是反過來說可能會有些道理。但是,你可能不希望_player_檢查任何聯繫人來簡化事情。 –