Presenting a scene in SpriteKit without discarding the previous?

﹥>﹥吖頭↗ 提交于 2019-12-04 09:34:40

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!