多次碰撞或吸毒

时间:2015-09-22 09:57:48

标签: ios sprite-kit

我有两种类型的碰撞。第一个是太空船和小行星(这已经处理过了)但是我无法处理第二种类型的碰撞,我的spaceShipLazer击中了敌人的空间。在第二种情况下,我有药物/多次碰撞或有时完美的碰撞。

我的enemySpaceShip是一个EnemySpaceShip类,它是SKSpriteNode的子类。

所以问题是关于第二次碰撞。是否有多次不期望的碰撞。我怎么解决它?

  func didBeginContact(contact: SKPhysicsContact) {

    if contact.bodyA.categoryBitMask == CollisionCategories.PlayerSpaceShip && contact.bodyB.categoryBitMask == CollisionCategories.Asteroid || contact.bodyB.categoryBitMask == CollisionCategories.PlayerSpaceShip && contact.bodyA.categoryBitMask == CollisionCategories.Asteroid {

      if !gameOver && !playerWasHit {

        playerWasHit = true
        self.pauseTheGame()

        //spaceShip vs asteroid animation
        let fadeOutAction = SKAction.fadeOutWithDuration(0.1)
        fadeOutAction.timingMode = SKActionTimingMode.EaseOut

        let fadeInAction = SKAction.fadeInWithDuration(0.1)
        fadeInAction.timingMode = SKActionTimingMode.EaseOut

        let blinkAction = SKAction.sequence([fadeOutAction, fadeInAction])
        let blinkRepeatAction = SKAction.repeatAction(blinkAction, count: 3)

        let delayAction = SKAction.waitForDuration(0.2)

        let gameOverAction = SKAction.runBlock({ () -> Void in

          self.gameSettings.lives--
          self.gameDelegate?.gameDelegateDidUpdateLives()

          if self.gameSettings.lives > 0 {

            self.respawn()
          } else {

            self.gameSettings.recordScores(self.gameSettings.currentScore)
            self.gameDelegate?.gameDelegateGameOver(self.gameSettings.currentScore)
            self.gameOver = true
            self.pauseTheGame()

          }
        })

        let gameOverSequence = SKAction.sequence([blinkRepeatAction, delayAction, gameOverAction])
        spaceShipLayer.runAction(gameOverSequence)
      }

      if soundOn {
        let hitSoundAction = SKAction.playSoundFileNamed("hitSound.wav", waitForCompletion: true)
        runAction(hitSoundAction)
      }
      //second case playerLazer vs enemySpaceShip
    } else if contact.bodyA.categoryBitMask == CollisionCategories.PlayerLaser && contact.bodyB.categoryBitMask == CollisionCategories.EnemySpaceShip || contact.bodyB.categoryBitMask == CollisionCategories.PlayerLaser && contact.bodyA.categoryBitMask == CollisionCategories.EnemySpaceShip {

        contact.bodyA.node?.removeFromParent()
        contact.bodyB.node?.removeFromParent()


      }
    }

1 个答案:

答案 0 :(得分:0)

我找到了让一切变得完美的决定:

只是第二部分应该是这样的: 如果

contact.bodyA.categoryBitMask == CollisionCategories.PlayerLaser &&
      contact.bodyB.categoryBitMask == CollisionCategories.EnemySpaceShip ||
      contact.bodyA.categoryBitMask == CollisionCategories.EnemySpaceShip &&
      contact.bodyB.categoryBitMask == CollisionCategories.PlayerLaser
    {

      contact.bodyA.node?.physicsBody?.categoryBitMask = CollisionCategories.None
      contact.bodyB.node?.physicsBody?.categoryBitMask = CollisionCategories.None
        contact.bodyA.node?.removeFromParent()
        contact.bodyB.node?.removeFromParent()
        self.addPoints(5)

    }
  }

所以我做了.None categoryBitMask并在碰撞检测后应用它,所以现在点和spriteNode行为是正确的!