Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

ios - Go back to view controller from SKScene

So when you create a project using the SpriteKit template. You have your View controller and your SKScene.

From my view controller I start my game with the code given by default and present the scene.

In my TCAViewcontroller.m

- (IBAction)startGame:(id)sender {

    NSLog(@"Start Game triggered");
    mainPageImage.hidden = 1;
    // Configure the view.
    // Configure the view after it has been sized for the correct orientation.
    SKView *skView = (SKView *)self.view;
    if (!skView.scene) {
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;

        // Create and configure the scene.
        TCAMyScene *theScene = [TCAMyScene sceneWithSize:skView.bounds.size];
        theScene.scaleMode = SKSceneScaleModeAspectFill;

        // Present the scene.
        [skView presentScene:theScene];

    }
}

When the user loses in the game I would like to dismiss the scene and go back to my view controller I have. I can't seem to find anything with my searches to going back to the original view controller, just pushing to a game over scene. But I don't want to push to another scene, just dismiss the current scene and go back to my TCAViewController. Please answer using code for clarification Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your scene needs to offer a line of communication to your controller to indicate that is finished. You could, for example, create a delegate protocol and corresponding property in your scene. An example:

@protocol TCAMySceneDelegate;

@interface TCAMyScene : SKScene

@property (nonatomic, weak> id<TCAMySceneDelegate> delegate;

@end

@protocol TCAMySceneDelegate <NSObject>
- (void)mySceneDidFinish:(TCAMyScene *)gameScene;
@end

Then, in the .m of your TCAMyScene

- (void)endTheGame {
    // Other game-ending code
    [self.delegate mySceneDidFinish:self];
}

In your view controller, set itself as the delegate for your scene and implement the method:

- (IBAction)startGame:(id)sender {
    // Other code

    TCAMyScene *theScene = [TCAMyScene sceneWithSize:skView.bounds.size];
    theScene.scaleMode = SKSceneScaleModeAspectFill;
    theScene.delegate = self;

    // Other code
}

- (void)mySceneDidFinish:(TCAMyScene *)myScene {
    // logic for dismissing the view controller
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...