1

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

4GetFullOf
  • 1,738
  • 4
  • 22
  • 47

1 Answers1

3

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
}
Wayne Hartman
  • 18,369
  • 7
  • 84
  • 116
  • this doesn't seem to work for me, mySceneDiDFinish is not being triggered, That first block of code goes in my TCAScene.h file correct? – 4GetFullOf Feb 20 '14 at 20:41
  • 1
    You need to set the delegate when you instantiate the `TCAMyScene`: `theScene.delegate = self` – Wayne Hartman Feb 20 '14 at 20:44
  • Oh of course. Thank you so much fantastic answer! This works wonderfully. One last question though Ive checked the class reference and can't seem to find some sort of dismiss function for the scene to end it when the endTheGame function is called. What is the best way to dismiss the scene? – 4GetFullOf Feb 20 '14 at 20:54
  • or is it better to remove the scene from the view in the mySceneDidFinish method back in the view controller? if so how? – 4GetFullOf Feb 20 '14 at 21:03
  • SKView class reference doesn't show a way to remove the scene once you present it? – 4GetFullOf Feb 20 '14 at 21:12
  • The `SKScene` will get torn down when the `SKView` is torn down, which will happen when your view controller is dismissed. – Wayne Hartman Feb 21 '14 at 00:28
  • What is the //logic for dismissing the view controller? – Siriss Feb 24 '15 at 14:52
  • Your not really dismissing a viewController its an SKScene, but I think its presented like a viewController (modally). So I would dismiss it like this: dismissViewControllerAnimated(true, completion: nil) – 3366784 Sep 04 '15 at 07:30