5

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.

aanrv
  • 2,159
  • 5
  • 25
  • 37
  • Kobold Kit has push/pop scene functionality with transitions, should be easy to extract/adapt: https://github.com/KoboldKit/KoboldKit/blob/master/KoboldKit/KoboldKitFree/Framework/View/KKView.m Note that "background" scenes are not active nor drawn while there is a scene "pushed on top of the most recent one". – CodeSmile Jul 16 '14 at 20:36
  • You could also try to setup a delegate to communicate the scenes back and forth so that your second scene (the level scene) has access to the menu scene. Also, all of your other levels would have the same delegate to that you could setup a single method in your menu to extract the information from every level. – romsearcher Jul 18 '14 at 01:43

1 Answers1

2

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
0x141E
  • 12,613
  • 2
  • 41
  • 54