Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Sprite Kit Game: Game Over scene instead of just a popup text

I'm following the Space Cat tutorial and I've run into a problem while trying to expand on the simple concepts in the tutorial.

I've created a Game Over Scene that plays a little game over animation and gives the player the option to tap to retry/return to the gameplay scene.

When the player loses, instead of the little "Game Over" popup in SpaceCat, I want to transition to the Game Over Scene.

This is the code that comes with Space Cat

 - (void) performGameOver {
    THGameOverNode *gameOver = [THGameOverNode >>gameOverAtPosition:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))];
    [self addChild:gameOver];
    self.restart = YES;
    self.gameOverDisplayed = YES;
    [gameOver performAnimation];

    [self.backgroundMusic stop];
   [self.gameOverMusic play];

I've tried changing that to this...

- (void) performGameOver {

    SKAction * loseAction = [SKAction runBlock:^{
        SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
        SKScene * gameOverScene = [[GameOverScene alloc] >>initWithSize:self.size];
        [self.backgroundMusic stop];
        [self.view presentScene:gameOverScene transition: reveal];
    }];
[self runAction:[SKAction sequence:@[loseAction]]];

    self.restart = YES;
    self.gameOverDisplayed = YES;
    [self.backgroundMusic stop];
}

This isn't working, and I'm not sure how to fix it. It seems like it should be a pretty easy fix. Anybody have any ideas for how to make this work within the Space Cat framework?

3 Answers

Stone Preston
Stone Preston
42,016 Points

You never run the action. you probably should not use an action to present the scene anyways. try it like this:

- (void) performGameOver {


    [self.backgroundMusic stop];
    self.restart = YES;
    self.gameOverDisplayed = YES;
    [self.backgroundMusic stop];

    SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
    GameOverScene *gameOverScene = [[GameOverScene alloc]  initWithSize:self.size];
    [self.view presentScene:gameOverScene transition: reveal];

}

Thanks for the quick reply!

Hmmm that's giving me an unknown exception. The simulator force quits when the game over is triggered. I'm not great at deciphering the error codes yet. Here's what it's giving me.

    return UIApplicationMain(argc, argv, nil, NSStringFromClass([THAppDelegate class]));
}

libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb)

Stone Preston
Stone Preston
42,016 Points

look in the debug console, there should be some more information in there that will tell you exactly what caused the exception. you may need to scroll up or down some to find it

Eh, it must be a problem with my Game Over Scene because if I sub THTitleScene in for GameOverScene it works just fine.

If I'm looking at the right place, this is what I see in the debug console.

argv char ** 0xbfffede0 0xbfffede0

*argv char * "/Users/creativeadmin/Library/Application Support/iPhone Simulator/7.1/Applications/0C47DEFF-DF15-4336-9367-79A28FFBA7D0/Noah7.app/Noah7" 0xbfffef4c

**argv char '/' '/'

argc int 1 1

Stone Preston
Stone Preston
42,016 Points

no thats not the info you need. It should say something like terminating app due to uncaught exception...then right after that show the method call that did it

Oh! Dang. That was easy. Got it fixed. Forgot to load the texture atlas when I imported the GameOverScene. Thanks Stone!