Friday, 4 March 2011


Hey Guys,

I know it has been a long tome I am not writing new stuffs here but I was pretty bussy about my new game "Zombie Ninja". It has been a good game and it is in review now. I ll let everyone know when it publishes.

Today I want to talk about game programming by using cocos2d library. If you have some knowledge about game programming for iphone you should probably heard about cocos2d. It is basically a library for you to help building 2d games.

I will share my free game "Crazy Ali"'s source code with you by expaining the stuffs. Actually I could implement this game more optimized but because I want to show you different features of cocos2d I did not try to optimise it.

if you wanna have a quick idea about cocos2d i refer you the manuel of cocos2d. It has pretty cool explaination but if you dont have idea about computer graphics or game programming it may be difficult for you.

So for now I want you to download cocos2d from its website and install it on your computer. The installation has been explained on the website pretty cool so I will not talk about how to install cocos2d.

After you downlad and install cocos2d to your comuter lets start a new project and call it "Crazy Ali". To do that run your xcode select "New Project" after that you may see on the left side "User Templates" tab and under it "coco2d version name" just click on it and select "cocos2d box2d Application" from entire templates. This will automatically create necessary files and references needed for you to build a box2d project.

So what is Box2d?
Box2d is the library for you to handle physics on your games. It is pretty great library to use physics in your game. The main idea of the game that we will implement is going to be about a guy collecting some stuff dropping down from the sky. Yes we could do the dropping animation in basic cocos2d without using box2d but box2d will make it easier to hadle for us.

So after you create your game with the name "Crazy Ali" just click on file under classes group there are some stuff in that file that I will explain for you.

In file you can see the init method. It is the initialize method basically. When this file runs this method will run first. So in init method you may see:

self.isTouchEnabled = YES; // to enable touches for the entire scene

self.isAccelerometerEnabled = YES; // to enable Accelerometer for the entire scene that we will not need. so you may delete it.

CGSize screenSize = [CCDirector sharedDirector].winSize;
CCLOG(@"Screen width %0.2f screen height %0.2f",screenSize.width,screenSize.height);
// to show the sizes of the entire screen on console.

b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);

so this two line may be more important than the previous ones. This two line will set the gravity for your world in the scene. Basically you can see a vector class called b2Vec2. This class is used for metre calculation in box2d. so when we define the gravity we choise this type and we set the gravity to -10f in the next line, when we set a b2Vec2 object we give two parameters which are float, the first one for x axis and the second one is for y axis. so gravity.Set(0.0f, -10.0f) means that we have a vector with zero value of x and -10 value of y, this is the magic number of gravity impulse in real world ;)

bool doSleep = true;

This line is not necessary but to make your game more optimized you can type this line because when the dynamic body objects, which you dont know what they are yet, are simply static in a while the library make them sleep and this can increase the performance of the game. (When you run the project after you did write some code you can see the body objects on screen and if they are moving colors of the objects are pinky but if they are stand statically the colors become green. This shows when they are sleeping and when they are awake.)

world = new b2World(gravity, doSleep);

Here we create our world. Do not thing you are the god!! but in your game you create a world object. so from now on every body object that have physics behavior will be in this world. I will talk about the structure later some more.


Here we set the physics for our world. Means that "yes my world will have physics.".

m_debugDraw = new GLESDebugDraw( PTM_RATIO );
        uint32 flags = 0;
        flags += b2DebugDraw::e_shapeBit;
//        flags += b2DebugDraw::e_jointBit;
//        flags += b2DebugDraw::e_aabbBit;
//        flags += b2DebugDraw::e_pairBit;
//        flags += b2DebugDraw::e_centerOfMassBit;

These lines are standing for us when we want to debug our game to test it out. With these lines actually you can see the shapes you created in your world. If you do not type these lines and if you do not attach a sprite to your shapes it is IMPOSSIBLE for you to see your shapes on your screen.

We set debug draw first then we set some flags for it. As you can notice some of the lines commented out. These are actually optional. e_shapeBit enumarator is standing for you to see your shapes in the world while you are runnung the game. I reccomend you to delete the comment of the second line e_jointBit. when you comment it out and run the game if you have some joints in your game you can see how they are acting. An yes we will have some joints in "Crazy Ali" game so just turn it on!! ;)

The rest I can easily tell you to erase till the line that is written "[self schedule: @selector(tick:)];"
We will keep building our game by ourselves so we dont need them.

I reccomend you to erase addNewSpriteWithCoords and accelerometer methods as well because we do not need them as well.

It is all for today and in the next post I will let you have some idea about box2d bodies and how to import them to our game. Till that time I reccomend you to have a look the box2d manuel as well.

See you later guys.

No comments:

Post a Comment