【发布时间】:2015-05-12 15:39:45
【问题描述】:
我创建了一个玩家必须收集硬币的游戏。现在由于某种原因,当屏幕上有很多硬币要抓取时,当船与硬币碰撞时,它会直接穿过当前与之碰撞的硬币并移除添加到屏幕上的最后一个硬币。我怎样才能使发生碰撞的实际硬币从屏幕上移除?
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 & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(obstacleCategory)) != 0 {
ship.removeFromParent()
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameOverScene(size: self.size)
self.view?.presentScene(scene, transition: reveal)
}
if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(coinCategory)) != 0 {
coin.removeFromParent()
playerScore = playerScore + 1
playerScoreUpdate()
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if currentTime - self.lastMissileAdded > 1 {
self.lastMissileAdded = currentTime + 1
self.addMissile()
}
// Current time + 6 takes longer to respawn coins
if currentTime - self.lastCoinAdded > 1 {
self.lastCoinAdded = currentTime + 0
self.addCoin()
}
// Current time + 6 takes longer to respawn coins
//if currentTime - self.lastDiamondAdded > 1 {
// self.lastDiamondAdded = currentTime + 1
//self.addDiamond()
//}
self.moveBackground()
self.moveObstacle()
self.moveCoin()
//self.moveDiamond()
}
【问题讨论】: