1
votes

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

1

1 Answers

3
votes

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
}