In the last article we introduced LevelHelper and SpriteHelper which played an important role in implementing the clone of Angry Birds game called Ghetto Birds. In this article we are going to implement collision detection between the bird and the poor pig. We are also going to demonstrate how to use animations for our game using LevelHelper and SpriteHelper applications.

Implementing Collision Detection:

The LevelHelper framework contains very useful wrapper methods on top of the Box2d framework. These wrapper methods makes it very easy to implement collision detection between different objects. In order to implement the collision detection we must assign a TAG to our sprite. This is accomplished by using the LevelHelper tool. Open the LevelHelper project that we developed in the last article and create and assign a TAG to all the sprites you want to take part in a collision. Take a look at the screenshot where a new TAG is assigned to the bird.



Follow the same procedure to create and assign a new tag “PIG” to the pig sprite. Save the project and also generate and the code again.

NOTE: You must generate the code again after creating the TAGS. This is because LevelHelper will create the TAG values as enumerated types which can be easily accessed in code.

Once the code has been generated you are ready to use collision detection in your application. To enable collision detection you need to call the LevelHelper method called “useLevelHelperCollisionHandling”.



After that you need to register the collisions between different sprites. This is where the TAGS are used as shown in the implementation below:



The collisionBetweenAngryBirdAndPig is the method that is invoked when they both collide. The implementation is shown below:



And that is it! Run the application and launch the bird by touching it. When it collides with the pig you will notice that the above method gets invoked.

Currently nothing happens on collision and the pig does not incur any damage. You can easily remove the pig on collision. Check out the following implementation:



Now when the collision happens the pig is removed from the layer. Although this works seamlessly but it looks kinda lame that the pig is removed without any effect. In the next section we are going to glorify the death of the pig by using animations.

Implementing Animation on Collision:

Animations can easily be created by using SpriteHelper application. Click the animations tab and then “Create new animation”. Give animation a name, we are going to use “PoofAnimation”. Next add images which will take part in the animation. The SpriteHelper will provide you a preview of the animation. Take a look at the screenshot below:



That is all you need to do to create animations using SpriteHelper. Now using the LevelHelper drag and drop the “poof1” image to the level that you are creating but make it invisible. The poof image is added so we can retrieve it in our code and start the animation.



The prepareAnimationName method of the LHSprite is responsible for loading the animation. The playAnimation actually plays the animation.

NOTE: As of this writing the LevelHelper API documentation was not updated to reflect different methods associated with animation.

The screenshot below shows the final frame of the poof animation after the bird collided with the pig.



One thing you will notice is that after the animation is completed the first frame of the animation still appears on the scene. It would be cool to have an option right in the SpriteHelper which will remove the animation frames from the scene once the animation is completed. In order to remove the first frame of the animation we will use the services of NSNotificationCenter as shown below:



Run the application again and now you will notice that when the bird collides with the pig the animation runs and this time it ends.    

Download Code:

The complete source code for the Ghetto Birds project can be downloaded from GitHub using the following URL:

https://github.com/azamsharp/Ghetto-Birds

Conclusion:

In this small series we learned how to get started with building a game like Angry Birds. We demonstrated how to use the LevelHelper and SpriteHelper to simplify the process of creating physics enabled applications.