title: The 'if' function not performing the code straight away
body: <pre><code>    import SpriteKit
  
  func +(left: CGPoint, right: CGPoint) -&gt; CGPoint {
    return CGPoint(x: left.x + right.x, y: left.y + right.y)
  }
  
  func -(left: CGPoint, right: CGPoint) -&gt; CGPoint {
    return CGPoint(x: left.x - right.x, y: left.y - right.y)
  }
  
  func *(point: CGPoint, scalar: CGFloat) -&gt; CGPoint {
    return CGPoint(x: point.x * scalar, y: point.y * scalar)
  }
  
  func /(point: CGPoint, scalar: CGFloat) -&gt; CGPoint {
    return CGPoint(x: point.x / scalar, y: point.y / scalar)
  }
  
  #if !(arch(x86_64) || arch(arm64))
  func sqrt(a: CGFloat) -&gt; CGFloat {
    return CGFloat(sqrtf(Float(a)))
  }
  #endif
  
  extension CGPoint {
    func length() -&gt; CGFloat {
      return sqrt(x*x + y*y)
    }
    
    func normalized() -&gt; CGPoint {
      return self / length()
    }
  }
  
  class GameScene: SKScene {
    
    struct PhysicsCategory {
      static let none      : UInt32 = 0
      static let all       : UInt32 = UInt32.max
      static let monster   : UInt32 = 0b1       // 1
      static let projectile: UInt32 = 0b10      // 2
      
    }
    
    // 1
    let player = SKSpriteNode(imageNamed: &quot;player&quot;)
    var monstersDestroyed = 0
    var scorelabel: SKLabelNode?
    var playerscore = 0
      var ammocount = 30
      var ammoLabel: SKLabelNode?
    
    override func didMove(to view: SKView) {
      // 2
      backgroundColor = SKColor.white
      // 3
      player.position = CGPoint(x: size.width * -0.45, y: size.height * -0.49+20)
      // 4
      addChild(player)
      
      scorelabel = childNode(withName: &quot;scoreLabel&quot;) as? SKLabelNode
      ammoLabel = childNode(withName: &quot;ammoLabel&quot;) as? SKLabelNode
      
      physicsWorld.gravity = .zero
      physicsWorld.contactDelegate = self
      
      run(SKAction.repeatForever(
        SKAction.sequence([
          SKAction.run(addMonster),
          SKAction.wait(forDuration: 1.0)
          ])
      ))
      
      
      let backgroundMusic = SKAudioNode(fileNamed: &quot;music.caf&quot;)
      backgroundMusic.autoplayLooped = true
      addChild(backgroundMusic)
        }
    
    func random() -&gt; CGFloat {
      return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
    }
    
    func random(min: CGFloat, max: CGFloat) -&gt; CGFloat {
      return random() * (max - min) + min
    }
    
    func addMonster() {
      // Create sprite
      let monster = SKSpriteNode(imageNamed: &quot;monster&quot;)
      
      monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size) // 1
      monster.physicsBody?.isDynamic = true // 2
      monster.physicsBody?.categoryBitMask = PhysicsCategory.monster // 3
      monster.physicsBody?.contactTestBitMask = PhysicsCategory.projectile // 4
      monster.physicsBody?.collisionBitMask = PhysicsCategory.none // 5
      
      // Determine where to spawn the monster along the Y axis
      let actualY = random(min: -frame.size.height/2+monster.size.height, max: frame.size.height/2-monster.size.height)
      
      // Position the monster slightly off-screen along the right edge,
      // and along a random position along the Y axis as calculated above
      monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)
      
      // Add the monster to the scene
      addChild(monster)
      
      // Determine speed of the monster
      let actualDuration = random(min: CGFloat(5.0), max: CGFloat(10.0))
      
      monster.physicsBody!.usesPreciseCollisionDetection = true
      // Create the actions
      let actionMove = SKAction.move(to: CGPoint(x: size.width * -0.5, y: actualY), duration: TimeInterval(actualDuration))
      let actionMoveDone = SKAction.removeFromParent()
      let loseAction = SKAction.run() { [weak self] in
        guard let `self` = self else { return }
        let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
          
          
          
          let gameOverScene = MenuScene(fileNamed: &quot;MenuScene&quot;)
          print(&quot;lost&quot;)
          gameOverScene?.scaleMode = .aspectFit
          self.view?.presentScene(gameOverScene!, transition: reveal)
          
          
      }
      monster.run(SKAction.sequence([actionMove, loseAction, actionMoveDone]))
      
      
    }
    
    override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      // 1 - Choose one of the touches to work with
      guard let touch = touches.first else {
        return
      }
      run(SKAction.playSoundFileNamed(&quot;throwingsound.mp3&quot;, waitForCompletion: false))
      
      let touchLocation = touch.location(in: self)
      
      // 2 - Set up initial location of projectile
      let projectile = SKSpriteNode(imageNamed: &quot;lazer&quot;)
      projectile.position =  CGPoint(x: size.width * -0.45, y: size.height * -0.49+20)
      
      projectile.physicsBody = SKPhysicsBody(circleOfRadius: projectile.size.width/2)
      projectile.physicsBody?.isDynamic = true
      projectile.physicsBody?.categoryBitMask = PhysicsCategory.projectile
      projectile.physicsBody?.contactTestBitMask = PhysicsCategory.monster
      projectile.physicsBody?.collisionBitMask = PhysicsCategory.none
      projectile.physicsBody?.usesPreciseCollisionDetection = true
      
      // 3 - Determine offset of location to projectile
      let offset = touchLocation - projectile.position
      
      // 4 - Bail out if you are shooting down or backwards
      if offset.x &lt; 0 { return }
      
      // 5 - OK to add now - you've double checked position
      addChild(projectile)
      ammocount -= 1
      ammoLabel?.text = &quot;ammo: \(ammocount)&quot;
      
      // 6 - Get the direction of where to shoot
      let direction = offset.normalized()
      
      // 7 - Make it shoot far enough to be guaranteed off screen
      let shootAmount = direction * size.width*2
      
      // 8 - Add the shoot amount to the current position
      let realDest = shootAmount + projectile.position
      
      // 9 - Create the actions
      projectile.physicsBody!.usesPreciseCollisionDetection = true
      
      let actionMove = SKAction.move(to: realDest, duration: 3)
      let actionMoveDone = SKAction.removeFromParent()
      projectile.run(SKAction.sequence([actionMove, actionMoveDone]))
      
    }
    
    func projectileDidCollideWithMonster(projectile: SKSpriteNode, monster: SKSpriteNode) {
      print(&quot;Hit&quot;)
      projectile.removeFromParent()
      monster.removeFromParent()
      
      
      playerscore += 1
      scorelabel?.text = &quot;Score: \(playerscore)&quot;
      
      if ammocount &lt; 25 {
        let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
          let gameOverScene = MenuScene(fileNamed: &quot;MenuScene&quot;)
          gameOverScene?.scaleMode = .aspectFit
          view?.presentScene(gameOverScene!, transition: reveal)
      }
    }
  }
  
  extension GameScene: SKPhysicsContactDelegate {
    func didBegin(_ contact: SKPhysicsContact) {
      // 1
      var firstBody: SKPhysicsBody
      var secondBody: SKPhysicsBody
      if contact.bodyA.categoryBitMask &lt; contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
      } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
      }
      
      // 2
      if ((firstBody.categoryBitMask &amp; PhysicsCategory.monster != 0) &amp;&amp;
        (secondBody.categoryBitMask &amp; PhysicsCategory.projectile != 0)) {
        if let monster = firstBody.node as? SKSpriteNode,
          let projectile = secondBody.node as? SKSpriteNode {
          projectileDidCollideWithMonster(projectile: projectile, monster: monster)
        }
      }
    }
  }
  </code></pre>
  <p>the if function when ammo count is below 25 does not execute the code straight away. It performs the code after a few seconds of ammocount going below 25. This code is an adaption of a tutorial I found online. Thank you for any help. I am adding these words as it says my post needs more details I will provide more details if someone asks tho.</p>