What's the best way to handle multiple SKScenes?
Solution 1:
Having a Single View Controller and Multiple Scenes
You could use single view controller (which is actually a default state of SpriteKit game template) and have multiple scenes.
So you will have GameViewController
and LandingScene
, GameScene
and possible some other scenes, like LevelSelect
scene or something like that.
In GameViewController
, you initialize your scene for the first time. So that is the point where you initialize your LandingScene
(I guess that is the place where you implemented your navigation menu).
So, from that point, you are able to make a transition from any scene you want using SKView's
presentScene: method (and optionally using SKTransition
class).
Transitioning
Transition in SpriteKit
can be generally done in two different ways:
1. Some might tell you that making a transition from a current scene, to the next scene is a "bad design" and that the current scene should notify the view controller about its ready-to-transition state, so that view controller can make needed transition. But in response to that, these are quotes from docs:
Transitioning Between Two Scenes
Typically, you transition to a new scene based on gameplay or user input. For example, if the user presses a button in your main menu scene, you might transition to a new scene to configure the match the player wants to play.
And the related code:
- (void)mouseUp:(NSEvent *)theEvent
{
[self runAction: self.buttonPressAnimation];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
GameConfigScene *newScene = [[GameConfigScene alloc] initWithSize: CGSizeMake(1024,768)]];
[self.scene.view presentScene: newScene transition: reveal];
}
It can be clearly seen that transition to the next scene is done within current scene.
2. Using the method described above using delegation pattern, where scene delegates responsibility of transitioning to the view controller.
Both ways are perfectly fine, where the first method is a bit convenient IMO, and widely used. So that is how you can navigate between different scenes in SpriteKit
in an easy way.
Hint:
Don't forget to override scene's dealloc
(or deinit
if you use Swift) method while in development phase, to make sure that all scenes are deallocated correctly.