Get Updates By E-mail 

Subscriber Counter
Article Written By:
Johann Dowa

Game Programming Tutorial – Beginners Action Game

Resetting The Game

The first thing we are do here is add a classwide boolean variable so that this code will not execute multiple times so place this code in the interface in the Game.h file.

BOOL resetButtonVisible;

The reason I added this was because if multiple balloons exit the screen at the same time the movementThroughTopOfScreen method is executed multiple times.

Now add this if statement into the movementThroughTopOfScreen method.

if(resetButtonVisible == NO)
{
resetButtonVisible = YES;

The reason I added this was because if multiple balloons exit the screen at the same time the movementThroughTopOfScreen method is executed multiple times.

Now we’re going to add a semi-transparent image covering the screen.

SPImage *backgroundImage = [SPImage imageWithContentsOfFile:@"screenoverlay.png"];
[playFieldSprite addChild:backgroundImage];

An added side effect of running this code is that touches are prevented from reaching any ballons.

Now we’re going to add a button to the center of the screen.

SPButton *resetButton = [SPButton buttonWithUpState:[SPTexture textureWithContentsOfFile:@"reset_button.png"]];
resetButton.x = self.width/2-resetButton.width/2;
resetButton.y = self.height/2-resetButton.height/2;
resetButton.fontName = @"Marker Felt";
resetButton.fontSize = 20;
resetButton.text = @"Reset Game";

Notice that in creating this button we needed to provide an image, however the animation causing the image to shrink is all built in to the Sparrow Framework. As you can see the properties of buttons are very similar to text fields.

Now we’re going to add an event listener to the button.

[resetButton addEventListener:@selector(onResetButtonTriggered:) atObject:self
forType:SP_EVENT_TYPE_TRIGGERED];

The triggered event occurs when the button is pressed.

Add the button to the playing field with this code:

[playFieldSprite addChild:resetButton];

Now we’re going to create the onResetButtonTriggered event which runs when the button is pressed.

This method is pretty simple we just clear the screen, and then make things just as they were when the game first started.

- (void)onResetButtonTriggered:(SPEvent*)event
{
[playFieldSprite removeAllChildren];

resetButtonVisible = NO;

level = 1;
score = 0;
levelTextField.text = [NSString stringWithFormat:@"Level: %d", level];
scoreTextField.text = [NSString stringWithFormat:@"Score: %d", score];

[self addBalloon];
}

And that’s the end, when you run this tutorial you should see a reset button and be able to restart the game after a ballon exits through the top of the screen like in the screenshot below:

You can download the completed project here with extensive comments in the Game implementation file.

I hope you enjoyed this tutorial please re-tweet and share it by using the buttons below. If you have any questions then please enter them in the comments below.  I’d also love to here what you’d like to see next with this (menu, highscores, powerups?).  Thanks.

Pages: 1 2 3 4 5 6

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments