不确定哪个部分是问题。我正在做一个spriteKit射击游戏,碰撞不起作用。宇宙飞船没有被摧毁。
它必须与某处的碰撞有关...
射弹只是略微移动敌人,但没有摧毁它们。
struct physicsCategory {
static let player : UInt32 = 1
static let enemy : UInt32 = 2
static let projectile : UInt32 = 3
}
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == physicsCategory.projectile) && (secondBody.categoryBitMask == physicsCategory.enemy) || (firstBody.categoryBitMask == physicsCategory.enemy) && (secondBody.categoryBitMask == physicsCategory.projectile)) {
projectileCollision(enemyTemp: firstBody.node as! SKSpriteNode, projectileTemp: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask == physicsCategory.enemy) && (secondBody.categoryBitMask == physicsCategory.player) || (firstBody.categoryBitMask == physicsCategory.player) && (secondBody.categoryBitMask == physicsCategory.enemy)) {
enemyPlayerCollision(enemyTemp: firstBody.node as! SKSpriteNode, playerTemp: secondBody.node as! SKSpriteNode)
}
}
///////////
func projectileCollision(enemyTemp: SKSpriteNode, projectileTemp: SKSpriteNode){
enemy.removeFromParent()
projectile.removeFromParent()
score = score + 1
updateScore()
}
////////////
func enemyPlayerCollision(enemyTemp: SKSpriteNode, playerTemp: SKSpriteNode) {
mainLabel.fontSize = 50
mainLabel.alpha = 1.0
mainLabel.text = "Game Over"
player.removeFromParent()
isAlive = false
waitThenMoveToTiltleScreen()
}
答案 0 :(得分:0)
您的第一个问题是static let projectile : UInt32 = 3
如果你想声明一个唯一的bitMasks,它需要2的幂(2 ^ 0,2 ^ 1,2 ^ 2,...等)
3真正的意思是,你的射弹既是玩家又是敌人(1 = 2 ^ 0,2 = 2 ^ 1)
你的第二个问题是你的逻辑
let f = firstBody.categoryBitMask
let s = secondbody.categoryBitmask
let p = physicsCategory.projectile
let x = physicsCategory.player
let e = physicsCategory.enemy
这是你的主张如果声明:
if ((f == p) && (s == e) || (f == e) && (s == p)) {
projectileCollision(enemyTemp: firstBody.node as! SKSpriteNode, projectileTemp: secondBody.node as! SKSpriteNode)
}
在你的if语句中,你说的是第一个或第二个可能是敌人,但在你的射弹碰撞方法中,你声称第一个身体永远是敌人,第二个身体总是射弹。
你需要处理你的第一个身体是射弹的状态,而你的第二个身体是敌人,因为很可能,你的射弹会进入敌人,所以它将成为第一个身体。
答案 1 :(得分:0)
谢谢(对不起,我对spriteKit相当新鲜)我认为我几乎在那里,因为我已经在下面编写了这段代码并且效果更好。
只有一个小问题!一些射弹只是通过敌人。只有一些毁灭。func didBegin(_ contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == physicsCategory.enemy) && (secondBody.categoryBitMask == physicsCategory.projectile) || (firstBody.categoryBitMask == physicsCategory.projectile) && (secondBody.categoryBitMask == physicsCategory.enemy)) {
projectileCollision(enemy: firstBody.node as! SKSpriteNode, projectile: secondBody.node as! SKSpriteNode)
}
else if ((firstBody.categoryBitMask == physicsCategory.enemy) && (secondBody.categoryBitMask == physicsCategory.player) || (firstBody.categoryBitMask == physicsCategory.player) && (secondBody.categoryBitMask == physicsCategory.enemy)) {
enemyPlayerCollision(enemy: firstBody.node as! SKSpriteNode, person: secondBody.node as! SKSpriteNode)
}
}
///////////
func projectileCollision(enemy: SKSpriteNode, projectile: SKSpriteNode){
enemy.removeFromParent()
projectile.removeFromParent()
score = score + 1
updateScore()
}
////////////
func enemyPlayerCollision(enemy: SKSpriteNode, person: SKSpriteNode) {
mainLabel.fontSize = 50
mainLabel.alpha = 1.0
mainLabel.text = "Game Over"
person.removeFromParent()
enemy.removeFromParent()
isAlive = false
waitThenMoveToTiltleScreen()
}