Get Updates By E-mail 

Subscriber Counter
Article Written By:
Johann Dowa

Game Programming Tutorial – Beginners Action Game

Triggering Events From Other Events

The first thing we are going to do is create a simple method with a for-loop to create more balloons as the level goes up.  Add the code below into the Game.m file, and declare the method in the Game.h file.

-(void)drawBalloons
{
for(int i = 0; i < level; i++)
{
[self addBalloon];
}
}

Now we’re going to add an event listener (similar to what we did with touches) so that we can execute some code when the balloon has exited the screen. Place this code at the bottom of the addBalloon method.

[tween addEventListener:@selector(movementThroughTopOfScreen:) atObject:self forType:SP_EVENT_TYPE_TWEEN_COMPLETED];

Noticed that I added this event listener to the tween variable that we created for the balloon’s rising animation. The tween completed event occurs when this animation completes and with this code the movementThroughTopOfScreen method will be executed.

Now we’re going to create the movementThroughTopOfScreen method, and make it so that the animation stops.  Add the following code into the Game.m file, and declare the movementThroughTopOfScreen method in the Game.h file.

-(void)movementThroughTopOfScreen:(SPEvent*)event
{
[self.juggler removeAllObjects];
}

Executing the removeAllObjects method on the juggler ends the execution of the tween’s that we created.

Now we’re going to add another listener this time to the end of the onTouchBalloon method.

[tween addEventListener:@selector(balloonPopped:) atObject:self forType:SP_EVENT_TYPE_TWEEN_COMPLETED];

We’re going to create the method for this listener now that executes when the balloon has completely fallen through the screen. This is the method header to add to the Game class files.

-(void)balloonPopped:(SPEvent*)event

We’re going to need to remove the balloon from the playfield so just as we did in the ontouchballon event we’re going to get the target only this time cast it is a tween so we’re going to need to cast it as such, and get the target of the tween which is the balloon, and what we really want.

SPTween *animation = (SPTween*)[event target];
SPDisplayObject *currentBalloon = (SPDisplayObject*)[animation target];

We’re going to remove the balloon completely from the playing field.

[playFieldSprite removeChild:currentBalloon];

Now if all balloons have been removed we need to increase the level, and send out more balloons onto the playing field. This is the code we’ll use to do that.

if(playFieldSprite.numChildren == 0)

The numChildren variable as you can probably guess tells us how many children a sprite has.

The rest of the code on this page will go within this if statement.

Let’s increase the level, and update the display.

level++;
levelTextField.text = [NSString stringWithFormat:@"Level: %d", level];

Now to start up the next level we are going to execute the drawBalloons method.

[self drawBalloons];

Now when we run the project as we pop balloons more will show up, and if one exits through the top of the screen the game will pause.

This screenshot shows how multiple balloons should appear on the screen once you start popping balloons:

You can download a project file that brings you to the end of this part of the tutorial here.

In the next part of the tutorial we’re going to add a reset button, and restart the game so we can keep playing.

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