由于运行应用程序时未捕获的异常而终止应用程序

Posted

技术标签:

【中文标题】由于运行应用程序时未捕获的异常而终止应用程序【英文标题】:Terminating app due to uncaught exception while running app 【发布时间】: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 函数上,因为通过注释掉该调用,我可以敲击按钮并永远更改颜色而不会出错 我添加到我的问题罗恩 【参考方案1】:

您正在尝试添加一个节点,但它已经有一个父节点。它就在错误消息中。

你可以检查它是否已经有这样的父母

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

if childNode.parent == nil 
    parentNode.addChild(childNode)

【讨论】:

这是一个很好的故障保险,但它并没有解决他为什么要尝试添加对象两次的问题。 是的,他不需要这样做,但如果他不想解决这个问题,他可以使用秋季保险箱 非常感谢罗恩的宝贵时间

以上是关于由于运行应用程序时未捕获的异常而终止应用程序的主要内容,如果未能解决你的问题,请参考以下文章

由于未捕获的异常 NSInvalidArgumentException 而终止应用程序,原因:索引无效

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序。无法识别的选择器发送到实例

*** 由于未捕获的异常“NSGenericException”而终止应用程序,原因:“开始日期不能晚于结束日期!”

由于未捕获的异常“com.firebase.durabledeeplink”而终止应用程序,原因:“配置持久深度链接失败

iOS5 和 XCode4 错误 - 由于未捕获的异常“NSUnknownKeyException”而终止应用程序

由于未捕获的异常“NSRangeException”而导致应用程序终止