0

I'm making my game in Xcode 9.0 (beta version) And basically, I'm trying to get a button in the End Scene to present the Game Scene when it is clicked, but whenever I click the button, the game crashes and it says this in the console: terminating with uncaught exception of type NSException.

Can someone please help me!?

Below is the code I use.

import SpriteKit

class EndScene: SKScene {

var replayButton:SKSpriteNode!

override func didMove(to view: SKView) {
    replayButton = self.childNode(withName: "replay") as! SKSpriteNode
    replayButton.position = CGPoint(x: 0, y: 0)
    replayButton.setScale(0.5)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if let location = touch?.location(in: self) {
        let nodesArray = self.nodes(at: location)

        if nodesArray.first?.name == "replay" {
            let transition = SKTransition.push(with: .right, duration: 1)
            let gameScene = GameScene(size: self.size)
            self.view?.presentScene(gameScene, transition: transition)
        }
    }
}

}

My code for the button

This is everything in the debug console when the app crashes:

2017-07-26 16:54:35.697622+0200 Sides[49264:5441087] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' texture:[ 'SidesBG1' (420 x 660)] position:{0, 0} scale:{1.00, 1.00} size:{2000, 2000} anchor:{0.5, 0.5} rotation:0.00' * First throw call stack: ( 0 CoreFoundation 0x000000010ae65c7b exceptionPreprocess + 171 1 libobjc.A.dylib 0x0000000106a80121 objc_exception_throw + 48 2 CoreFoundation 0x000000010aed7a45 +[NSException raise:format:] + 197 3 SpriteKit 0x00000001077b26a1 -[SKNode insertChild:atIndex:] + 162 4 SpriteKit 0x00000001077b25de -[SKNode addChild:] + 68 5 Sides 0x000000010614d796 _T05Sides9GameSceneC7didMoveySo6SKViewC2to_tF + 7398 6 Sides 0x000000010614da2c _T05Sides9GameSceneC7didMoveySo6SKViewC2to_tFTo + 60 7 SpriteKit 0x0000000107779d2d -[SKScene _didMoveToView:] + 204 8 SpriteKit 0x0000000107799e00 -[SKView presentScene:transition:] + 347 9 Sides 0x0000000106156b42 _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtF + 1730 10 Sides 0x0000000106156dc8 _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtFTo + 104 11 SpriteKit 0x0000000107798181 -[SKView touchesBegan:withEvent:] + 1130 12 UIKit 0x0000000107a5f998 -[UIWindow _sendTouchesForEvent:] + 2130 13 UIKit 0x0000000107a61360 -[UIWindow sendEvent:] + 4124 14 UIKit 0x0000000107a074d2 -[UIApplication sendEvent:] + 352 15 UIKit 0x0000000108306384 __dispatchPreprocessedEventFromEventQueue + 2809 16 UIKit 0x0000000108308eeb __handleEventQueueInternal + 5957 17 CoreFoundation 0x000000010ae093a1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 18 CoreFoundation 0x000000010adedc8f __CFRunLoopDoSources0 + 271 19 CoreFoundation 0x000000010aded23f __CFRunLoopRun + 1039 20 CoreFoundation 0x000000010adecbb9 CFRunLoopRunSpecific + 409 21 GraphicsServices 0x000000010fd4d9e2 GSEventRunModal + 62 22 UIKit 0x00000001079eacb2 UIApplicationMain + 159 23 Sides 0x00000001061559d7 main + 55 24 libdyld.dylib 0x000000010bee4b95 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

  • can you add the stack trace? – sarosh mirza Jul 26 '17 at 14:26
  • ...and the description of the exception from the debug log. – Phillip Mills Jul 26 '17 at 14:27
  • What do you mean – Yohannes Gobezie Jul 26 '17 at 14:27
  • This is what it says in the debug log: libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) – Yohannes Gobezie Jul 26 '17 at 14:28
  • 2
    You should put your actual code in the question and not an image of it. Welcome to SO. Please take a look at this [stackoverflow.com/help/how-to-ask](http://stackoverflow.com/help/how-to-ask) and this [stackoverflow.com/help/mcve](http://stackoverflow.com/help/mcve) for best results when using this site. – MwcsMac Jul 26 '17 at 14:32
  • You can found the trace in the debug console In bottom of XCode when your app is running. – pierreafranck Jul 26 '17 at 14:37
  • This is everything in the debug console: – Yohannes Gobezie Jul 26 '17 at 14:49
  • 2017-07-26 16:48:51.160451+0200 Sides[48965:5406187] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: name:'(null)' texture:[ 'SidesBG0' (417 x 657)] position:{0, 0} scale:{1.00, 1.00} size:{2000, 2000} anchor:{0.5, 0.5} rotation:0.00' *** First throw call stack: ( 0 CoreFoundation 0x0000000112b1cc7b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x00000001108e4121 objc_exception_throw + 48 – Yohannes Gobezie Jul 26 '17 at 14:49
  • 2 CoreFoundation 0x0000000112b8ea45 +[NSException raise:format:] + 197 3 SpriteKit 0x00000001116166a1 -[SKNode insertChild:atIndex:] + 162 4 SpriteKit 0x00000001116165de -[SKNode addChild:] + 68 5 Sides 0x000000010ffb1996 _T05Sides9GameSceneC7didMoveySo6SKViewC2to_tF + 7398 6 Sides 0x000000010ffb1c2c _T05Sides9GameSceneC7didMoveySo6SKViewC2to_tFTo + 60 – Yohannes Gobezie Jul 26 '17 at 14:50
  • 7 SpriteKit 0x00000001115ddd2d -[SKScene _didMoveToView:] + 204 8 SpriteKit 0x00000001115fdc3c -[SKView presentScene:] + 532 9 Sides 0x000000010ffbab5e _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtF + 1710 10 Sides 0x000000010ffbade8 _T05Sides8EndSceneC12touchesBeganys3SetVySo7UITouchCG_So7UIEventCSg4withtFTo + 104 11 SpriteKit 0x00000001115fc181 -[SKView – Yohannes Gobezie Jul 26 '17 at 14:51
  • It's obvious your `GameScene` is reusing some nodes that are already presented, probably the background. – Sulthan Jul 26 '17 at 15:03
  • Oh, but what do I do then – Yohannes Gobezie Jul 26 '17 at 16:10
  • @Sulthan that seems to fix the problem! :) But when I go back to the game scene, everything is too big. – Yohannes Gobezie Jul 26 '17 at 16:33

2 Answers2

0

Try updating didMove to the following

    var replayButton: SKSpriteNode!
override func didMove(to view: SKView) {

    if (self.childNode(withName: "//replay") as? SKSpriteNode) != nil {
        replayButton = self.childNode(withName: "replay") as! SKSpriteNode
        replayButton.position = CGPoint(x: 0, y: 0)
        replayButton.setScale(0.5)

    }
}


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if let location = touch?.location(in: self){
        let nodesArray = self.nodes(at: location)

        if nodesArray.first?.name == "replay"{
            let transition = SKTransition.push(with: .right, duration: 1)
            let gameScene = GameScene(size: self.size)
            self.view?.presentScene(gameScene, transition: transition)
        }
    }
}

I edited the code, check if this works without the crash and doing the action on the button.

sarosh mirza
  • 702
  • 5
  • 12
0

Never mind, I fixed it! It was because it was adding nodes that already existed, so I did removeFromParent to all my objects (except one, which were the enemies) and it worked(but the objects were still too big when I went back, so I did wrote this when transitioning to my EndScene: let endScene = EndScene(fileNamed: "EndScene") endScene!.size = self.size endScene!.scaleMode = self.scaleMode).

And wrote this when transitioning to my end scene: let gameScene = GameScene(fileNamed: "GameScene") gameScene!.size = self.size gameScene!.scaleMode = self.scaleMode )

Huge thank you to @Sulthan!