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)
}
}
}
}
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)