Question

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 allocand 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!

Was it helpful?

Solution

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];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top