Implementing Kraazy Koala Using Cocos2d, LevelHelper and SpriteHelper Part 3
In the last article we mentioned that our beloved Koala will go on a war against the crazy monkey. In this article we will enter into the field and explain how to create the epic battle.
Hanging Monkey with Joints:
Monkeys don't just sit around, they hang upside down with the trees and imitate Tarzan by swinging on the branches. We can do the same in our game by using the power of joints. Creating joints in LevelHelper is pretty simple. All you need to do is to select the type of joint to create and then select the bodies involved and that is it! A joint can only be applied to a physics body. This means that the sprite body has to be either static or dynamic. Although we can create a joint between the tree and the monkey but we will use a different approach. We will create a joint between a block and the monkey. The block will be invisible so that it does not make our tree ugly. We will explain the reason for our approach later in this article.
The screenshot below shows how to make a joint between the block and the monkey. Make sure that the block is static and the monkey is dynamic.
Run the level using the LevelHelper and you will notice that now the monkey swings from left to right like a ... monkey.
NOTE: If LevelHelper does not load your most recent changes then close the SceneTester and then test the level again. There is a glitch in SceneTester which prevents it from getting the latest changes.
Currently, the monkey is unarmed but that is about to change. We will give an unlimited supply of bananas to the monkey. The bananas are thrown at regular interval which is scheduled using the NSTimer class as shown in the implementation below:
The above code will trigger the throwBanana method every 8 seconds. The throwBanana method is implemented below:
We used the createSpriteWithName method to fetch the banana directly from the SpriteHelper file. Make sure that the banana sprite is set to dynamic in the SpriteHelper file. We are using the ApplyLinearImpulse to apply force to the banana using the randomly generated vector forces. Another important thing to notice is that we are assigning the BANANA tag to the banana sprite. This will play an important role if you want to detect collision between the banana and the koala.
Pineapples cannot kill a monkey! We need explosives! In this section we are going to create a dynamite line using the path options provided in LevelHelper. We will put an explosive under the tree and then smoke the monkey.
Drag the explosive from the sprites icon view and place it under the tree. Use the Bezier tool to create a path from the koala to the explosive as shown in the screenshot below:
To make the bezier a path simply check the box in the right pane. Also give the bezier a unique name "DynamiteLine".
At this point we have laid out the dynamite line and we are ready to spark it. Drag the spark (fire) sprite from the sprite asset view and put it beside the beginning of the dynamite line.
The sprite "fire" is set to follow the path called DynamiteLine. This is done by setting the "fire" sprite property as shown in the screenshot below:
If you run the level in LevelHelper you will realize that the fire sprite starts following the path but when it reaches the end nothing happens. We need some sort of notification to trigger when the fire sprite reaches the end of the path. This is where the NSNotificationCenter comes in for the rescue. The implementation below how to attach the path end notification using the NSNotificationCenter.
The firePathHasEnded method simply performs some explosions and make sure that the monkey falls into the burning fire!
The complete code for the Kraazy Koala can be downloaded from Github using the following link:
In this three part series we learned how to make a complete game using Cocos2d, LevelHelper, SpriteHelper and Kobold2d. The game covered many different aspects of LevelHelper and SpriteHelper framework. We really hope that you liked the series.