Search code examples
arraysswiftspawnsknode

"Attemped to add a SKNode which already has a parent" in Swift


I have this function which I am using to generate Flower objects at random locations every second:

func spawnFlower() {
    //Create flower with random position
    let tempFlower = Flower()
    let height = UInt32(self.size.height / 2)
    let width = UInt32(self.size.width / 2)
    let randomPosition = CGPoint(x: Int(arc4random_uniform(width)), y: Int(arc4random_uniform(height)))
    tempFlower.position = randomPosition

    var tooClose = false
    flowerArray.append(tempFlower)

    // enumerate flowerArray
    for flower in flowerArray {

        // get the difference in position between the current node
        // and each node in the array
        let xPos = abs(flower.position.x - tempFlower.position.x)
        let yPos = abs(flower.position.y - tempFlower.position.y)

        // check if the spawn position is less than 10 for the x or y in relation
        // to the current node in the array
        if (xPos < 10) || (yPos < 10) {
            tooClose = true
        }

        if tooClose == false {
            //Spawn node
            addChild(tempFlower)
        }
    }
}

I am creating a new instance for flower every time the function is called, but for some reason when I call the function like below, it gives me the error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent:

The spawnFlower() function is called every second. It works the first time it is called, and the second time it crashes. What am I doing wrong?


Solution

  • The addChild() call needs moving outside of the for loop, so that tempFlower is added to its parent only once.

    func spawnFlower() {
        //Create flower with random position
        let tempFlower = Flower()
        let height = UInt32(self.size.height / 2)
        let width = UInt32(self.size.width / 2)
        let randomPosition = CGPoint(x: Int(arc4random_uniform(width)), y: Int(arc4random_uniform(height)))
        tempFlower.position = randomPosition
    
        var tooClose = false
        flowerArray.append(tempFlower)
    
        // enumerate flowerArray
        for flower in flowerArray {
    
            // get the difference in position between the current node
            // and each node in the array
            let xPos = abs(flower.position.x - tempFlower.position.x)
            let yPos = abs(flower.position.y - tempFlower.position.y)
    
            // check if the spawn position is less than 10 for the x or y in relation
            // to the current node in the array
            if (xPos < 10) || (yPos < 10) {
                tooClose = true
            }
        }
    
        if tooClose == false {
            // Spawn node
            addChild(tempFlower)
        }
    }