문제

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!

도움이 되었습니까?

해결책

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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top