newtocoding
newtocoding

Reputation: 109

Why do i get this error in swift xcode? "Attemped to add a SKNode which already has a parent"

I dont know why I keep getting this error message. Can you take a look at my code and tell me what Im doing wrong? Im getting an error on the arrowLeft for some reason.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' texture:[

class GameScene: SKScene, SKPhysicsContactDelegate {

let arrowLeft = SKSpriteNode(imageNamed: "leftarrow")
let arrowRight = SKSpriteNode(imageNamed: "rightarrow")

func moveArrowInstructions() {

    arrowLeft.position = CGPointMake(self.size.width / 4.0, self.size.height / 2)
    arrowLeft.zPosition = 20
    arrowRight.position = CGPointMake(self.size.width / 1.3, self.size.height / 2)
    arrowRight.zPosition = 20
    arrowLeft.setScale(0.4)
    arrowRight.setScale(0.4)

    arrowRight.alpha = 0
    arrowLeft.alpha = 0

    let moveArrowToRight = SKAction.moveByX(150 + arrowRight.size.width, y: 0, duration: 2.5)
    let moveArrowToLeft = SKAction.moveByX(-150 - arrowLeft.size.width, y: 0, duration: 2.5)
    let fadeInArrows = SKAction.fadeInWithDuration(1.0)
    let fadeOutArrows = SKAction.fadeOutWithDuration(1.0)
    let sequenceLeftArrow = SKAction.sequence([fadeInArrows, moveArrowToLeft, fadeOutArrows])
    let moveLeftArrowRepeat = SKAction.repeatActionForever(sequenceLeftArrow)
    let sequenceRightArrow = SKAction.sequence([fadeInArrows, moveArrowToRight, fadeOutArrows])
    let moveRightArrowRepeat = SKAction.repeatActionForever(sequenceRightArrow)

    arrowLeft.runAction(moveLeftArrowRepeat)
    arrowRight.runAction(moveRightArrowRepeat)




    addChild(arrowLeft)
    addChild(arrowRight)
}


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

        func moveArrows() {

        let generateArrows = SKAction.sequence([
            SKAction.runBlock(self.moveArrowInstructions),
            SKAction.waitForDuration(3.5)])
        let endlessAction = SKAction.repeatActionForever(generateArrows)
        runAction(endlessAction)
    }

    var touch: UITouch = touches.first as! UITouch
    var location = touch.locationInNode(self)
    var node = self.nodeAtPoint(location)


    if(node.name == "startgame") {
        arrowRight.removeFromParent()
        arrowLeft.removeFromParent()






    }

Upvotes: 1

Views: 129

Answers (2)

martin meincke
martin meincke

Reputation: 86

You are attempting to add the same node to the scene multiple times.

A quick fix could be to check if the node has already been added to the scene. like so:

if arrowLeft.parent == nil && arrowRight.parent == nil{
   addChild(arrowLeft)
   addChild(arrowRight)
}

Upvotes: 2

Knight0fDragon
Knight0fDragon

Reputation: 16847

you are constantly adding the same child to the parent every touch

addChild(arrowLeft) addChild(arrowRight)

needs to be in an area when setting up your scene

Upvotes: 1

Related Questions