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.
for(int i = 0; i < level; i++)
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.
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.
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.
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.
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.
SPDisplayObject *currentBalloon = (SPDisplayObject*)[animation target];
We’re going to remove the balloon completely from the playing field.
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.
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.
levelTextField.text = [NSString stringWithFormat:@"Level: %d", level];
Now to start up the next level we are going to execute the drawBalloons method.
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:
In the next part of the tutorial we’re going to add a reset button, and restart the game so we can keep playing.
10 11 10 11
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.