Adding Touch Events
To program in our response to the balloon being touched we are going to use the Sparrow Framework’s event system. Add this code to the end of the addBalloon method.
There are different SPEvent types, and SP_EVENT_TYPE_TOUCH is run whenever a touchable object is touched.
You can see that we used the selector for a method called “onTouchBalloon” this method will be executed whenever a touch occurs.
We will create the onTouchBalloon method as specified as the event listener, add this into both the Game.h and Game.m files.
Notice that this method takes an SPTouchEvent.
We will need to respond whenever a balloon is first touched, and the first thing we will need to do is determine which object has been touched.
We will remove the touch listener similar to how we added it simply by changing the word add to remove.
We are going to place the following code to increase the score and update the score text.
scoreTextField.text = [NSString stringWithFormat:@"Score: %d", score];
We’re also going to play a popping sound.
Notice that all I did here was execute the play method on the SPSound. We don’t need to do any more here as we don’t need to loop or change the volume as we did with the background music. This is the simplest way to play a sound using the Sparrow Framework.
Let’s stop the upward animation of the balloon by removing all animations related to the touched balloon from the Juggler.
Now we’re going to change the scale of the balloon to give the balloon a thinning effect like the air is flowing out from it.
[tween animateProperty:@"scaleX" targetValue:0.75];
[tween animateProperty:@"scaleY" targetValue:1.25];
Notice that I changed the scale of the balloon by adjusting the scaleX and scaleY properties.
Now we’re going to send the balloon out through the bottom of the screen.
[tween animateProperty:@"y" targetValue:self.height+currentBalloon.height];
If everything is done right when the project is run a single balloon should be sent up the screen, and fall through the bottom of the screen when touched like in this screenshot:
If you have any problems you can download a project file to bring you to this point here.
In the next section we are going to draw more balloons as they are popped and increase the score and level.
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.