所以我正在研究这款“太空射击游戏”游戏,我遇到了一些错误,这些错误并没有让我的游戏正常运行/崩溃。
所以第一个问题是当我在“didBeginContact”中使用我创建的函数时,我的游戏有时会崩溃。这是“didBeginContact”中的代码
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCatagory.fAsteroids) && (secondBody.categoryBitMask == PhysicsCatagory.Asteroids) || (firstBody.categoryBitMask == PhysicsCatagory.Asteroids) && (secondBody.categoryBitMask == PhysicsCatagory.fAsteroids)){
asteroidsHitEachOther(firstBody.node as! SKSpriteNode, FuriousAsteroids: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask == PhysicsCatagory.Asteroids) && (secondBody.categoryBitMask == PhysicsCatagory.Bullet) || (firstBody.categoryBitMask == PhysicsCatagory.Bullet) && (secondBody.categoryBitMask == PhysicsCatagory.Asteroids)){
bulletHitAsteroids(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
}
else if ((firstBody.categoryBitMask == PhysicsCatagory.fAsteroids) && (secondBody.categoryBitMask == PhysicsCatagory.Bullet) || (firstBody.categoryBitMask == PhysicsCatagory.Bullet) && (secondBody.categoryBitMask == PhysicsCatagory.fAsteroids)){
bulletHitfAsteroids(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
}
if ((firstBody.categoryBitMask == PhysicsCatagory.Asteroids) && (secondBody.categoryBitMask == PhysicsCatagory.Fighter) || (firstBody.categoryBitMask == PhysicsCatagory.Fighter) && (secondBody.categoryBitMask == PhysicsCatagory.Asteroids)){
fighterGetHit(firstBody.node as! SKSpriteNode, Asteroids: secondBody.node as! SKSpriteNode)
}
else if ((firstBody.categoryBitMask == PhysicsCatagory.fAsteroids) && (secondBody.categoryBitMask == PhysicsCatagory.Fighter) || (firstBody.categoryBitMask == PhysicsCatagory.Fighter) && (secondBody.categoryBitMask == PhysicsCatagory.fAsteroids)){
fighterGetHitHard(firstBody.node as! SKSpriteNode, fAsteroids: secondBody.node as! SKSpriteNode)
}
}
因此游戏有时会因这些错误而崩溃。 Error #1 Error #2
游戏崩溃的线是这些线。
asteroidsHitEachOther(firstBody.node as! SKSpriteNode, FuriousAsteroids: secondBody.node as! SKSpriteNode)
bulletHitAsteroids(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
bulletHitfAsteroids(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
fighterGetHit(firstBody.node as! SKSpriteNode, Asteroids: secondBody.node as! SKSpriteNode)
fighterGetHitHard(firstBody.node as! SKSpriteNode, fAsteroids: secondBody.node as! SKSpriteNode)
现在,如果你想看看我创建函数的方式,那么你可以在这里查看所有函数。
func asteroidsHitEachOther(Asteroids: SKSpriteNode, FuriousAsteroids: SKSpriteNode){
Asteroids.removeFromParent()
FuriousAsteroids.removeFromParent()
}
func bulletHitAsteroids(Asteroids: SKSpriteNode, Bullet: SKSpriteNode){
Asteroids.removeFromParent()
Bullet.removeFromParent()
Points++
pointLabel.text = "Points: \(Points)"
}
func bulletHitfAsteroids(fAsteroids: SKSpriteNode, Bullet: SKSpriteNode){
fAsteroids.removeFromParent()
Bullet.removeFromParent()
Points = Points + 2
pointLabel.text = "Points: \(Points)"
}
func fighterGetHit(Fighter: SKSpriteNode, Asteroids: SKSpriteNode){
Asteroids.removeFromParent()
HitPoints--
hpLabel.text = "\(HitPoints) HP"
}
func fighterGetHitHard(Fighter: SKSpriteNode, fAsteroids: SKSpriteNode){
fAsteroids.removeFromParent()
HitPoints = HitPoints - 2
hpLabel.text = "\(HitPoints) HP"
}
现在,在我们谈到第一个错误之后,我将讨论第二个错误,它无法正常运行游戏。
现在我们可以看到,在上面的函数中,在“bulletHitfAsteroids”中,points变量被假设为两个。但是当我玩这个游戏时,当我击中fAsteroids时,积分只上升了一个。对于“fighterGetHitHard”来说,情况也是如此,健康状况不会下降两个,而是只下降一个。
我很感激能得到的帮助,谢谢。