Step5: Understanding the First project[Bubble Burst]

So now you have a basic idea of cocos2dx. If not also just proceed and you get it when we write code practically. Lets see about our game first...
GameName : Bubble Burst
Concept : There will be a lot of bubbles we need to click/touch to burst them.
Why we need to do such a easy game?
Is this what you are thinking?
Yea such simple game will avoid us from getting in to complicated game-logics and so we can focus full on learning cocos2dx.

Now lets see the project(BubbleBurst) we had created using cocos2dx in visualstudio 2010.
Open the solution and set  BubbleBurst.win32 as "startup project".

Directory structure and file list of the  project.

BubbleBurst\Classes : have all the .cpp and .h files and all the code goes here.
BubbleBurst\ Resources : have all the images, sounds and other resources needed for our game.
BubbleBurst\ proj.win32 : have the main.cpp, main.h, Visualstudio project files(BubbleBurst.win32.vcxproj).

Code Explanation (not all the lines are explained if u have more doubts ping me)

Main.cpp
1.>Create an instance of the application in the main()
AppDelegate app;

2.> Set the window title and window size by
eglView.setViewName("Bubble Burst");
eglView.setFrameSize(800, 600);



3.>Get current application instance and start the game by
CCApplication::sharedApplication().run();



Class  AppDelegate
This class inherits the property of the  CCApplication class. And over ride functions like "applicationDidEnterBackground", "applicationDidFinishLaunching" etc..
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();
virtual bool applicationDidFinishLaunching();
virtual void applicationDidEnterBackground();
virtual void applicationWillEnterForeground();
};


And will create the "director " and maintains all scenes of the game.
CCDirector *pDirector = CCDirector::sharedDirector();

Functions like "applicationDidEnterBackground()" and " applicationWillEnterForeground() " used for pausing and resuming sounds, animations etc. You can also show a pause menu if the game is minimized or send to background. 



Class HelloWorld
This class is the main class of the game. This is inherited from a Layer class(CCLayer) and have a function scene which will return a scene object to the "Director" in the Class AppDelegate.
CCScene* HelloWorld::scene()

On the Init() function MenuItem, Label and Images are added to this Helloworld layer
Creating menu :

CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));




A menu item with Image(normal and mouse over) is added. And on clicking on that image (click event), it will call a function HelloWorld::menuCloseCallback. In that function code for handling the click event is writen.

Creating Label: 

CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

This is how a label with text "Hello World", font "Arial" of size "24" is  created.


Creating Image/Sprite: 
CCSprite* pSprite = CCSprite::create("HelloWorld.png");


we have to pass the image name to Create function in the class CCSprite to create and draw an image.

OK we had created various items like menu item, label, sprite.
Now we need to position them on the screen like
pLabel->setPosition(ccp(size.width / 2, size.height - 50));
and finally we need to add it to the layer to draw on the screen like
this->addChild(pLabel, 1);


Now you might got some clear picture of the code and cocos2dx. Now lets roll and start making the real game.
Go to Tutorial Index

No comments:

Post a Comment