title: Swift SpriteKit Game gives error 'Attempted to add a SKNode which already has a parent'
body: <p>I have a SpriteKit game and I am using a SKAction to move my meteor (the meteors are the enemies) all the way to the end of the screen where the player (the player is at the bottom of the screen, and they can only move on the x-axis to dodge meteors not the y) must dodge these meteors. Right I'm only trying to get one meteor on the screen at a time which moves towards the direction of the player using SKAction. There should only be on meteor at a time on the screen, but I get the  error 'Attempted to add a SKNode which already has a parent'. My SKAction adds the meteor to the screen but then deletes it after the meteor has moved to its destination which is all then way to the bottom of the screen. So why am I getting this error and how can I fix it?</p>
  
  <p>NOTE: The SKAction removes the meteor from the screen by using meteor.removeFromParent()</p>
  
  <p>Here is my code: </p>
  
  <pre><code>import SpriteKit
  import GameplayKit
  import UIKit
  
  
  
  class GameScene: SKScene, SKPhysicsContactDelegate{
  
      let player = SKSpriteNode(imageNamed: "spaceship")
      let stars = SKSpriteNode(imageNamed: "stars")
      let meteor = SKSpriteNode(imageNamed: "meteor")
      var scoreLabel = SKLabelNode(fontNamed: "AmericanTypewriter-Bold")
      var score:Int = 0
      var playerLost:Bool = false
  
  
      override func didMove(to view: SKView) {
  
          physicsWorld.contactDelegate = self
          print(frame.size.width)
          print(frame.size.height)
  
          stars.position = CGPoint(x:0, y:0)
          stars.zPosition = 1
  
          player.position = CGPoint(x:0, y:-320)
          player.zPosition = 4
          player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
          player.physicsBody?.affectedByGravity = false
          player.physicsBody?.isDynamic = false
  
          player.physicsBody?.categoryBitMask = 2
          player.physicsBody?.collisionBitMask = 2
          player.physicsBody?.contactTestBitMask = 1
  
  
  
          self.addChild(player)
          self.addChild(stars)
  
          self.addMeteor()
          self.setLabel()
  
          //spawnEnemySKAction()
  
  
      }
  
  
      func spawnEnemySKAction() {
          let spawn = SKAction.run(addMeteor)
          let waitToSpawn = SKAction.wait(forDuration: 1)
          let spawnSequence = SKAction.sequence([spawn, waitToSpawn])
          let spawnForever = SKAction.repeatForever(spawnSequence)
          self.run(spawnForever)
      }
  
  
      func didBegin(_ contact: SKPhysicsContact) {
          gameOver()
  
      }
  
      func addMeteor() {
  
          meteor.physicsBody = SKPhysicsBody(texture: meteor.texture!, size: meteor.size)
          meteor.physicsBody?.affectedByGravity = false
          meteor.setScale(0.50)
          meteor.position = CGPoint(x:Int(arc4random()%300),y:Int(arc4random()%600))
          //meteor.position = CGPoint(x:0 , y:0)
          meteor.zPosition = 4
  
          let moveMeteor = SKAction.moveTo(y: player.position.y - 300, duration: 1.5)
          let deleteMeteor = SKAction.removeFromParent()
          let meteorSequence = SKAction.sequence([moveMeteor, deleteMeteor])
          meteor.run(meteorSequence)
  
          meteor.physicsBody?.categoryBitMask = 1
          meteor.physicsBody?.collisionBitMask = 2
          meteor.physicsBody?.contactTestBitMask = 2
  
          self.addChild(meteor)
          }
  
      func fireBullet() {
          let bullet = SKSpriteNode(imageNamed: "bullet")
          bullet.position = player.position
          bullet.setScale(0.5)
          bullet.zPosition = 3
          self.addChild(bullet)
          let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
          let deleteBullet = SKAction.removeFromParent()
          let bulletSequence = SKAction.sequence([moveBullet, deleteBullet])
          bullet.run(bulletSequence)
  
      }
      func setLabel() {
          scoreLabel.text = "Score: " + String(score)
          scoreLabel.position = CGPoint(x: 290, y: 590)
          scoreLabel.zPosition = 20
      }
  
      func gameOver() {
          playerLost = true
          var gameOverLabel: SKLabelNode!
          gameOverLabel = SKLabelNode(fontNamed: "AmericanTypewriter-Bold")
          gameOverLabel.text = "Game Over! You lost! Your score was: " + String(score)
          gameOverLabel.zPosition = 10
          gameOverLabel.position = CGPoint(x: 0, y:0)
          self.addChild(gameOverLabel)
          player.removeFromParent()
          meteor.removeFromParent()
          stars.removeFromParent()
          scoreLabel.removeFromParent()
  
      }
  
  
      override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
          if playerLost == false {
          fireBullet()
          }
      }
  
      override func touchesMoved(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
          for touch:  AnyObject in touches {
              let pointOfTouch = touch.location(in: self)
              let previousPointOfTouch = touch.previousLocation(in: self)
              let amountDragged = pointOfTouch.x - previousPointOfTouch.x
              player.position.x += amountDragged
          }
  
      }
  
  
      override func update(_ currentTime: TimeInterval) {
          addMeteor()
          if meteor.position.y &lt; player.position.y - 300 &amp;&amp; playerLost == false {
              meteor.removeFromParent()
              addMeteor()
              if playerLost == false {
                  score += 1
              }
              scoreLabel.removeFromParent()
              scoreLabel.text = "Score: " + String(score)
              self.addChild(scoreLabel)
          }
      }
  }
  </code></pre>
  
  <p>Thanks for taking the time to look at this question!</p>