Diego Benítez Diego Benítez - 5 months ago 15
iOS Question

Terminating app due to uncaught exception while running app

I am making a game in which if I press a button the player (circuloPrincipal) changes to the color to a color determinate by the button.
When I run my game sometimes crashes with the error below. I havent find the error, because I cont know how to interpretate

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'button4' texture:[ 'circuloBoton' (100 x 100)] position:{407, 39} scale:{1.00, 1.00} size:{60, 60} anchor:{0.5, 0.5} rotation:0.00'

circuloPrincipal.size = CGSize(width: 225, height: 225)
circuloPrincipal.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
circuloPrincipal.color = colorAzul
circuloPrincipal.colorBlendFactor = 1.0
circuloPrincipal.physicsBody = SKPhysicsBody(circleOfRadius: circuloPrincipal.size.height / 2)
circuloPrincipal.physicsBody?.contactTestBitMask = physicsCategory.circuloPrincipal
circuloPrincipal.physicsBody?.collisionBitMask = physicsCategory.enemigo
circuloPrincipal.physicsBody?.contactTestBitMask = physicsCategory.enemigo
circuloPrincipal.physicsBody?.affectedByGravity = false
circuloPrincipal.physicsBody?.dynamic = false
circuloPrincipal.name = "circuloPrincipal"
circuloPrincipal.zPosition = 5.0
circuloBlanco.size = CGSize(width: 60, height: 60)
circuloBlanco.position = CGPoint(x: frame.width / 2 - 105 , y: frame.height / 2 - 345)
circuloBlanco.color = colorBlanco
circuloBlanco.colorBlendFactor = 1.0
circuloBlanco.zPosition = 4.0

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

/* Called when a touch begins */

if gameStarted == false {

circuloVerde.removeFromParent()
circuloMorado.removeFromParent()
circuloRojo.removeFromParent()
circuloBlanco.removeFromParent()


circuloPrincipal.color = colorAzul

enemigoTimer = NSTimer.scheduledTimerWithTimeInterval(0.7, target: self, selector: Selector("enemigos"), userInfo: nil, repeats: true)

gameStarted = true
circuloPrincipal.runAction(SKAction.scaleTo(0.44, duration: 0.4))
score = 0
scoreLabel.text = "\(score)"
hits = 0
highscoreLabel.runAction(SKAction.fadeOutWithDuration(0.5))
}

circuloVerde.name = "button"

let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)

if let name = touchedNode.name {
if name == "button" {
circuloPrincipal.color = colorVerde
}
}

circuloAmarillo.name = "button2"

let touch2 = touches.first
let positionInScene2 = touch2!.locationInNode(self)
let touchedNode2 = self.nodeAtPoint(positionInScene2)

if let name = touchedNode2.name {
if name == "button2" {
circuloPrincipal.color = colorAmarillo
}
}

circuloAzul.name = "button3"

let touch3 = touches.first
let positionInScene3 = touch3!.locationInNode(self)
let touchedNode3 = self.nodeAtPoint(positionInScene3)

if let name = touchedNode3.name {
if name == "button3" {
//do my stuff
circuloPrincipal.color = colorAzul
}
}

circuloBlanco.name = "button4"

let touch4 = touches.first
let positionInScene4 = touch4!.locationInNode(self)
let touchedNode4 = self.nodeAtPoint(positionInScene4)

if let name = touchedNode4.name {
if name == "button4" {
circuloPrincipal.color = colorBlanco
}
}

circuloRojo.name = "button5"

let touch5 = touches.first
let positionInScene5 = touch5!.locationInNode(self)
let touchedNode5 = self.nodeAtPoint(positionInScene5)

if let name = touchedNode5.name {
if name == "button5" {
circuloPrincipal.color = colorRojo
}
}

circuloMorado.name = "button6"

let touch6 = touches.first
let positionInScene6 = touch6!.locationInNode(self)

let touchedNode6 = self.nodeAtPoint(positionInScene6)

if let name = touchedNode6.name {
if name == "button6" {
circuloPrincipal.color = colorMorado
}
}
}


func enemigos() {

let enemigo = SKSpriteNode(imageNamed: "enemigo")
enemigo.size = CGSize(width: 25, height: 25)
enemigo.color = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
enemigo.colorBlendFactor = 1.0
enemigo.zPosition = 3.0

enemigo.physicsBody = SKPhysicsBody(circleOfRadius: enemigo.size.height / 2)
enemigo.physicsBody?.categoryBitMask = physicsCategory.enemigo
enemigo.physicsBody?.contactTestBitMask = physicsCategory.circuloPrincipal
enemigo.physicsBody?.collisionBitMask = physicsCategory.circuloPrincipal
enemigo.physicsBody?.dynamic = true
enemigo.physicsBody?.affectedByGravity = true


var colors = [colorAzul, colorAmarillo]


if score >= 21 { colors.append(colorBlanco) }; if score >= 41 { colors.append(colorRojo) }; if score >= 71 { colors.append(colorMorado) }; if score >= 111 { colors.append(colorVerde) };let randomColorIndex = GKRandomDistribution(lowestValue: 0, highestValue: colors.count - 1).nextInt(); enemigo.color = colors[randomColorIndex]

if score == 20{

let fadingAnimation = SKAction.sequence([SKAction.fadeInWithDuration(0.7), SKAction.fadeOutWithDuration(0.7)])

linea = SKSpriteNode(imageNamed: "linea")

linea.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 160)
linea.size = CGSize(width: frame.width, height: 50)
linea.alpha = 0.3
linea.zPosition = 1.0


linea.runAction(fadingAnimation)


self.addChild(linea)

textLabel = SKLabelNode(fontNamed: "STHeitiJ-Medium")

textLabel.text = "New color added"
textLabel.fontSize = 35
textLabel.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 147)
textLabel.fontColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
textLabel.colorBlendFactor = 1.0
textLabel.zPosition = 2.0

textLabel.runAction(fadingAnimation)

self.addChild(textLabel)


}

if score == 40 {


let fadingAnimation = SKAction.sequence([SKAction.fadeInWithDuration(0.7), SKAction.fadeOutWithDuration(0.7)])



linea = SKSpriteNode(imageNamed: "linea")

linea.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 160)
linea.size = CGSize(width: frame.width, height: 50)
linea.alpha = 0.3
linea.zPosition = 1.0


linea.runAction(fadingAnimation)


self.addChild(linea)

textLabel = SKLabelNode(fontNamed: "STHeitiJ-Medium")

textLabel.text = "New color added"
textLabel.fontSize = 35
textLabel.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 147)
textLabel.fontColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
textLabel.colorBlendFactor = 1.0
textLabel.zPosition = 2.0

textLabel.runAction(fadingAnimation)

self.addChild(textLabel)


}

if score == 70 {

let fadingAnimation = SKAction.sequence([SKAction.fadeInWithDuration(0.7), SKAction.fadeOutWithDuration(0.7)])



linea = SKSpriteNode(imageNamed: "linea")

linea.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 160)
linea.size = CGSize(width: frame.width, height: 50)
linea.alpha = 0.3
linea.zPosition = 1.0
linea.runAction(fadingAnimation)


self.addChild(linea)

textLabel = SKLabelNode(fontNamed: "STHeitiJ-Medium")
textLabel.text = "New color added"
textLabel.fontSize = 35
textLabel.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 147)
textLabel.fontColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
textLabel.colorBlendFactor = 1.0
textLabel.zPosition = 2.0

textLabel.runAction(fadingAnimation)

self.addChild(textLabel)


}


if score == 110 {

let fadingAnimation = SKAction.sequence([SKAction.fadeInWithDuration(0.7), SKAction.fadeOutWithDuration(0.7)])

linea = SKSpriteNode(imageNamed: "linea")
linea.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 160)
linea.size = CGSize(width: frame.width, height: 50)
linea.alpha = 0.3
linea.zPosition = 1.0
linea.runAction(fadingAnimation)


self.addChild(linea)
textLabel = SKLabelNode(fontNamed: "STHeitiJ-Medium")
textLabel.text = "New color added"
textLabel.fontSize = 35
textLabel.position = CGPoint(x: frame.width / 2, y: frame.height / 2 + 147)
textLabel.fontColor = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1.0)
textLabel.colorBlendFactor = 1.0
textLabel.zPosition = 2.0
textLabel.runAction(fadingAnimation)
self.addChild(textLabel)


}

if score == 20 {

self.addChild(circuloBlanco)

}

if score == 40 {
self.addChild(circuloRojo)
}

if score == 70 {
self.addChild(circuloMorado)
}

if scoreLabel == 110 {
self.addChild(circuloVerde)
}



let posisionRandom = arc4random() % 4

switch posisionRandom {
case 0:

enemigo.position.x = 0
let posisionY = arc4random_uniform(UInt32(frame.size.height))
enemigo.position.y = CGFloat(posisionY)
self.addChild(enemigo)
break

case 1:

enemigo.position.y = 0
let posisionX = arc4random_uniform(UInt32(frame.size.width))
enemigo.position.x = CGFloat(posisionX)
self.addChild(enemigo)


break

case 2:

enemigo.position.y = frame.size.height
let posisionX = arc4random_uniform(UInt32(frame.size.width))
enemigo.position.x = CGFloat(posisionX)
self.addChild(enemigo)
break

case 3:

enemigo.position.x = frame.size.width
let posisionY = arc4random_uniform(UInt32(frame.size.height))
enemigo.position.y = CGFloat(posisionY)
self.addChild(enemigo)
break

default:
break
}

enemigo.runAction(SKAction.moveTo(circuloPrincipal.position, duration: 1.7))


}

Answer

you're trying to add a node but it already has a parent. its right there in the error message.

you can check to see if it already has a parent like this

let childNode = SKNode()
let parentNode = SKNode()

if childNode.parent == nil {
    parentNode.addChild(childNode)
}
Comments