Get Updates By E-mail 

Article Written By:
Johann Dowa

Game Programming Tutorial – Beginners Action Game


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.

[image addEventListener:@selector(onTouchBalloon:) atObject:self forType:SP_EVENT_TYPE_TOUCH];

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.

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

We will remove the touch listener similar to how we added it simply by changing the word add to remove.

[currentBalloon removeEventListener:@selector(onTouchBalloon:) atObject:self forType:SP_EVENT_TYPE_TOUCH];

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.

[[SPSound soundWithContentsOfFile:@"balloonpop.caf"] play];

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.

[self.juggler removeTweensWithTarget:currentBalloon];

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.

SPTween *tween = [SPTween tweenWithTarget:currentBalloon time:0.35 transition:SP_TRANSITION_LINEAR];
[tween animateProperty:@"scaleX" targetValue:0.75];
[tween animateProperty:@"scaleY" targetValue:1.25];
[self.juggler addObject:tween];

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 = [SPTween tweenWithTarget:currentBalloon time:(self.height-currentBalloon.y)/self.height transition:SP_TRANSITION_LINEAR];
[tween animateProperty:@"y" targetValue:self.height+currentBalloon.height];
[self.juggler addObject:tween];

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.


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.