0

I am having difficulty writing my app in xCode. I am creating a game and I have a scene for the game, and a scene for a shop that I am working on. My line to go to the shop is

let transition2 = SKTransition.fadeWithColor(UIColor.greenColor(), duration: 1.0) scene?.view?.presentScene(shopScene(), transition: transition2)

and this seems to work fine. But when I wish to transition back to the game scene, here is everything I have tried ( i commented it out so it wouldn't crash and I just stay on the shop scene)

// shopScene.removeFromParent(self.scene) // self.shopScene.removeFromParent() // self.view?.presentScene(GameScene()) // let gameScene = GameScene(size: theView.bounds.size) // gameScene.scaleMode = SKSceneScaleMode.ResizeFill // theView.presentScene(gameScene) //scene?.view?.willMoveToSuperview(GameViewController()) // scene?.view. //scene?.view?.presentScene(GameScene()) //scene?.view?.presentScene(GameScene()) // let desiredScene = GameScene2(size: (self.view?.scene?.size)!) // self.scene?.removeFromParent() // self.scene?.view?.removeFromSuperview() // self.scene?.view?.presentScene(GameScene2()) //self.scene?.view?.willMoveToSuperview(GameScene2()) //self.scene?.hidden = true

and almost everything I try gets the error

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attemped to add a SKNode which already has a parent: <SKNode> name:'(null)' position:{0, 0} scale:{1.00, 1.00} accumulatedFrame:{{0, 0}, {0, 0}}' *** First throw call stack: (0x180fc6e38 0x18062bf80 0x180fc6d80 0x18eede438 0x18eede364 0x1000e93d8 0x1000efbd4 0x18eeb14e0 0x18eece278 0x1000e6260 0x1000e62dc 0x18eeb14e0 0x18eece278 0x1000fa96c 0x1000faaf0 0x18eecccec 0x186147dc0 0x186140b08 0x186110f4c 0x18610f528 0x180f7d124 0x180f7cbb8 0x180f7a8b8 0x180ea4d10 0x18278c088 0x186179f70 0x1000fd130 0x180a428b8) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)

any help with this would be much appreciated.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    Don't get me wrong, but most of that commented code doesn't make sense. For example this doesn't make sense: self.shopScene.removeFromParent() because the scene should be a root node and can't have a parent. If you have added a scene to another scene, the added scene will act as ordinary node... If you are reusing the same scene, the error your seeing can happen… And something like this scene?.view?.presentScene(shopScene(), transition: transition2) looks shady. What method shopScene() actually does? – Whirlwind Apr 25 '16 at 23:57
  • 1
    Anyways, if you want to do it correctly, look at this example : http://stackoverflow.com/questions/35404455/whats-the-best-way-to-handle-multiple-skscenes/35409586#35409586 and abandon what you are doing currently. Also put some print statements in scene's deinit() methods to see if everything is deallocated properly. – Whirlwind Apr 25 '16 at 23:58

0 Answers0