Array of enemies and missiles

Hello,


i'm developing a spatial game by using SpriteKit and Swift. I have an array of enemies and an array of missiles. My goal is: when i get closer to an enemy he shoot a missile, when the missile reaches a specific distance from the enemy it is removed from the scene and the enemy shoots another missile. The array of missiles is initially empty, then when the player get closer to an enemy (by a function that calculate the distance between 2 nodes in the scene) i call a function that istantiate a missile and add it to the missiles array. And so far everything works properly. Then I have a function that remove the missile when it reaches a specified distance from the enemy that fired it. Here is the function:


func removeMissile() {
       
        for n in nemici {
           
            for m in missili {
               
                let dis = CGPointDistance(from: n.position, to: m.position)
               
                if dis >= 600 {
                   
                    m.removeFromParent()
                    enemyShoot = false
                }
               
            }
        }
       
    }
  


The problem is that if i call this function the missile does not appear In the scene. This because i should know which enemy of the Enemy Array has actualy fired the missile... how can i find that?


i hope that someone will be able to help me.


Best regards.

Replies

You could store that info in the Missile class. But its far easier if you just remove the missle when its outside of the view of the camera.