My situation is that I have a GameMenuScene
and after the user chooses a level, I want to present the LevelScene
. But I do not want to have the previous GameMenuScene
discarded because the LevelScene
is actually a @property
of GameMenuScene
and whether or not the user completes the level is to be saved as a @property
of LevelScene
, which the GameMenuScene
should be able to access after the user finishes or exits the level. If I simply use presentScene:transition
, the GameMenuScene
is discarded and the information cannot pass back.
My question: Is there a way to stack or push the scenes on top of each other without discarding the previous (preferably using a transition animation)? I could not find a method for this specific purpose in the Apple Documentation for SKScene.
Note: Another StackOverflow answer to a similar question suggests to create a new UIViewController
, present the LevelScene
in there and then present the UIViewController
modally. But I was hoping there was an existing method in the Apple Documentation that I have missed to present the scene itself scene modally without having to create UIVIewControllers
.
There is no navigation controller-like capability for SKScenes that allows you to push and pop scenes. You will need to write code to manage and present your scenes.
Here's a simple view controller implementation that allows you to switch between two scenes (by swiping) without discarding the other scene.
@interface ViewController()
@property BOOL viewFlag;
@property SKScene *scene1;
@property SKScene *scene2;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeGesture];
// Create and configure scene 1.
SKScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.scene1 = scene;
// Present the scene 1.
[skView presentScene:scene];
// Create and configure scene 2.
scene = [MySecondScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.scene2 = scene;
}
- (void) handleSwipeGesture:(id)sender
{
SKView * skView = (SKView *)self.view;
_viewFlag = !_viewFlag;
if (_viewFlag) {
[skView presentScene:_scene1];
}
else {
[skView presentScene:_scene2];
}
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With