Restarting
To restart the game from the end game scene, you need to modify EndGameScene
so it transitions back to the main game scene upon a touch event.
Open EndGameScene.m and add the following import at the top:
#import "MyScene.h"
Now add the following method to handle touch events:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Transition back to the Game
SKScene *myScene = [[MyScene alloc] initWithSize:self.size];
SKTransition *reveal = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:myScene transition:reveal];
}
This code simply transitions to a new MyScene
in much the same way you transition to this scene when the app first starts.
Build and run, and tap to start. Play the game and fall to reveal the end game scene. Tap the screen and watch the game restart.
Whoops, you forgot to reset the score! Remedy that now by stopping the app and going to MyScene.m. In initWithSize:
, where you reset _maxPlayerY
, add the following code:
[GameState sharedInstance].score = 0;
_gameOver = NO;
This resets the score in the GameState
singleton and also resets the _gameOver
flag.
Build and run again. Tap to start and play the game. When you get to the end game scene, whether from top or bottom, tap to try again. This time, the score resets to zero.
Now go and beat that high score! ;]
Where to Go From Here?
Congratulations, you’ve made a game like Mega Jump!
Here is a sample project with all of the code from this tutorial series.
In the 2 parts of this series you've covered the whole process of creating a physics based game. You learned much about how to setup collisions and how to build your game logic around detecting contacts in the game.
But you can do much more for your Uber Jump game! Open up the plist level data and think of new levels. What new shapes could you implement? You can just copy the plist source code and create any number of levels by adjusting what's inside the level file.
Play some Mega Jump and get inspiration! Can you implement some better game feat compared to the original game?
And last but not least if you want to learn more about Sprite Kit, check out the iOS Games by Tutorials book, where you’ll learn how to make five complete games—from zombie action to car racing to shootouts in space!
In the meantime, if you have any questions or comments, please join the forum discussion below.