I have two different view controllers with a SKScene
in the first one. There we find the game itself and in the second one the score with a "Replay" button. When the game finishes, I delete the scene and I alloc
and init
it again when the user clicks "Replay". My problem is that if a certain SKSpriteNode
was rotated when the game ended, after deleting it and reseting it, when I go back to that scene it remains rotated in the exact same angle as before deleting the scene. The -initWithSize
is called properly, so I'm not sure why is that happening. My question is if there's a way of resetting the SKScene
so that it's identical to the first time the game was played. Thanks!
Asked
Active
Viewed 4,544 times
4

IOS_DEV
- 949
- 3
- 12
- 33
-
Put `NSLog` in Scene's `dealloc` method. It seems your scene is not deallocating. – Andrey Gordeev Apr 22 '14 at 08:44
-
1You can simply reload the scene using `presentScene` – ZeMoon Apr 22 '14 at 09:09
-
@AndreyGordeev I've just tried that and the `dealloc` method is being called. – IOS_DEV Apr 22 '14 at 16:14
-
@akashg I already do that when I'm in the second view controller and I want to go back to the game scene. – IOS_DEV Apr 22 '14 at 16:15
1 Answers
4
yes, create a method that creates the scene in the first place and recall it. If you have objects that are properties remove them from parent before recalling your scene creation method.
Example:
-(id)initWithSize:(CGSize)size {
[super initWithSize:size];
[self startNewGame];
return self;
}
-(void)startNewGame {
//all your scene configs here
}
-(void)buttonThatsCalledOnRestartClick {
[self startNewGame];
}
the reason you're seeing your sprite in the same spot is because you're not removing it from the scene when you restart. Since you should probably just completely rebuild your scene you should remove all objects from scene and then call your "startNewGame" method again.
Example of removing all objects from scene:
for (SKNode* node in self.children) {
[node removeFromParent];
}

John Riselvato
- 12,854
- 5
- 62
- 89
-
1You better use one compound layer for your scene and delete all child nodes with removeAllChildren method of SKNode class. – AndrewShmig Apr 24 '14 at 12:58