【发布时间】:2016-06-24 16:57:43
【问题描述】:
我正在制作一个游戏,如果我按下按钮,玩家 (circuloPrincipal) 会将颜色更改为按钮确定的颜色。 当我运行我的游戏时,有时会因以下错误而崩溃。我没有找到错误,因为我不知道如何解释
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“尝试添加已具有父级的 SKNode:名称:'button4' 纹理:['circuloBoton' (100 x 100)] 位置:{407, 39 } 比例:{1.00, 1.00} 大小:{60, 60} 锚点:{0.5, 0.5} 旋转: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
}
}
}
【问题讨论】:
-
没有冒犯,但你的代码是一团糟。在要求人们阅读之前,您应该考虑清理所有额外的空白并对其进行格式化。
-
我正在尽我所能运行您的代码,并且运行良好。但是缺少大量代码(我只是注释掉了受影响的行)。你能把 enemigos 函数贴出来,让我看看里面发生了什么吗?
-
当然,罗恩有时会发生这种情况
-
这就是为什么我认为问题出在 enemigos 函数上,因为通过注释掉该调用,我可以敲击按钮并永远更改颜色而不会出错
-
我添加到我的问题罗恩
标签: ios sprite-kit