
Switch to Hud.mm and replace its contents with the following: you also need a method to change the current health. Additionally, the HUD needs to keep track of the current health (which you will need later) and the sprite representing each point of the monkey's health. (void) setHealth:(float) HUD display uses the sprites from the jungle sprite sheet, so you have to derive the HUD from CCSpriteBatchNode in order to have access to the jungle sprite sheet sprites. #define MAX_HEALTH_TOKENS Hud : CCSpriteBatchNodeĬCSprite *healthTokens // weak references Replace the contents of the Hud.h file with the following: Name the class Hud, and make it a subclass of CCSpriteBatchNode. Each banana represents 10 points of health.Ĭreate a new file with the iOS\Cocoa Touch\Objective-C class template.

You're going to represent the monkey's health with 10 banana icons. Let's add a health display so that you can keep track of the monkey's health. Yes, the monkey dies, but no one knows when it's going to happen! That's too realistic for me and most other players. The level should now restart two seconds after the monkey's death. In case of a restart, you simply remove all objects from the GB2Engine and replace the current scene with a new GameLayer.īuild and run.
#Physicseditor update#
A simple restart will suffice.Īdd a new variable to GameLayer.h to hold the restart timer:ĬcTime gameOverTimer // timer for restart of the levelĪnd add these lines to the beginning of update inside GameLayer.mm: Usually, we'd go to a high score table after a game ends, but that's too much for this tutorial. I would suggest restarting two seconds after the monkey's death.

Now the monkey dies, but the objects keep falling and there's no way to restart the game. Now let's put the hurt on the monkey by modifying the section with the head collision in beginContactWithObject:Įlse if(.y ĭisable the updateCCFromPhysics contents by changing section #1, as well:īuild and run, and now you can bring out your evil side - kill the monkey! :] Restarting the Game In the init selector, initialize the health with the maximum value by adding this below the game layer storage code:
#Physicseditor code#
Implement the isDead property by adding the following code above the walk method :Īs you'll notice, you decide if the monkey is dead or not based on if his health is less than 0 or not. Now switch to Monkey.mm and synthesize the health health Open Monkey.h and add a new variable called "health" to the Monkey class.Īlso add properties to access the health level and to detect if the monkey is (readonly) float (readonly) bool isDead įinally, add a define for the maximum health, at the top of the file below the import statements: Play the game for a bit and you'll realize that there isn't much challenge to this game – the monkey climbs but doesn't take any damage.
#Physicseditor free#
Feel free to try out other detection methods to see which one works best for you. For example, you could check how high the objects are piled, or if the monkey's speed is low for a certain amount of time. There are other ways to detect if the monkey is caught. That's much better! Now, if the monkey is caught in a trap and he can't push his way out, he will be magically freed. StuckWatchDogFrames = 120 // 2 seconds at 60fpsīuild and run. teleport the monkey above the highest object if they are some distance below the monkeyįloat prune =. 10 - Iterate over objects and turn objects into static objects you will use it to create a routine that checks the y-coordinates of all objects and puts to sleep any that are a certain distance below the monkey.Īdd the following code to the end of the update selector in GameLayer.mm: GB2Engine has an iterate method that can be used to iterate all objects with a block. you will use this feature to improve performance.

As a result, a falling statue will only affect the top of the stack instead of the complete pile.īox2d allows objects to go to sleep when they aren’t touched by other objects for some time. These static objects will still let other objects pile up above them, but they’ll no longer react to the impact. To improve the situation, you’re going to convert objects which are some distance below the monkey into static objects. If you watch a statue drop, you’ll see that nearly the entire stack below moves and bounces from impulses passed from the statue down through the stack from one object to another. Box2d uses hashes to make things faster, but you can imagine how dramatically the number of collisions increases as the number of objects increases. If there are n objects, there are n*(n-1) possible collisions to handle. All of these collisions have to be handled by Box2d. There’s a reason for this – as objects continue to fall from the sky, one after another, they bump into the objects already lying around.
