HelloWorld Engineering Analysis for Cocos2d-X3.0
Open the HelloWorld project in the previous blog and you will see the project file.
Code in the main. cpp file (I have commented on it)
# Include "main. h "# include" AppDelegate. h "# include" cocos2d. h "// namespace USING_NS_CC; // main function of the Cocos2d-X (equivalent to the main function in C/C ++) int APIENTRY _ tWinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {// two useless parameters: UNREFERENCED_PARAMETER (hPrevInstance); UNREFERENCED_PARAMETER (lpCmdLine); // defines an app object AppDelegate app; // execute the run function of the app object. Return Application: getInstance ()-> run ();}
The code in main. cpp only implements the following operations:
1. Define an App object
5. Execute the App object to enter the frame loop
Note: what really matters in the program is the run function in the last line of code. The run function plays a crucial role in subsequent game development.
Code in the AppDelegate. cpp file (I have commented on it)
# Include "AppDelegate. h "# include" HelloWorldScene. h "// namespace USING_NS_CC; // constructor AppDelegate: AppDelegate () {}// destructor AppDelegate ::~ AppDelegate () {}// function bool AppDelegate: applicationDidFinishLaunching () {// initialize the director auto Director = director: getInstance (); // obtain the OpenGL view auto glview = director-> getOpenGLView (); // if the OpenGL view is not obtained, if (! Glview) {// create an OpenGL view glview = GLView: create ("My Game"); // set the OpenGL view director-> setOpenGLView (glview );} // set whether to display the debugging information director-> setDisplayStats (true); // set the frame rate director-> setAnimationInterval (1.0/60); // call the scenario auto scene = HelloWorld :: createScene (); // execution scenario director-> runWithScene (scene); return true ;} // The function called when the program enters the background (when someone calls the game, the program enters the background) void AppDelegate: applicationDidEnterBackground () {// stop playing the animation ctor:: getInstance ()-> stopAnimation (); // pause playing background music // SimpleAudioEngine: getInstance ()-> pauseBackgroundMusic ();} // function called when the program is re-activated (sound replaying) void AppDelegate: applicationWillEnterForeground () {// playback animation ctor: getInstance () -> startAnimation (); // continue playing background music // SimpleAudioEngine: getInstance ()-> resumeBackgroundMusic ();}The code in AppDelegate. cpp mainly implements operations performed after the game is started. operations performed after the game is started:
1. initialize the director class
2. Obtain the previously created OpenGL View
3. If the OpenGL view is not obtained, recreate the OpenGL view.
4. Set openGL View
5. Set whether to display debugging information.
5. Set the number of animation Frames
6. Call scenario (the real start of the game)
7. execution scenario
Code in HelloWorldScene. h (I have commented)
# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "// The HelloWorld class inherits from the Layer class HelloWorld: public cocos2d: Layer {public: // creation scenario static cocos2d: Scene * createScene (); // initialization layer virtual bool init (); // menu response function void menuCloseCallback (cocos2d: Ref * pSender); // used to create: scenario, menu, layer, and other things CREATE_FUNC (HelloWorld) ;};# endif
Code in HelloWorldScene. cpp (I have commented)
# Include "HelloWorldScene. h "// namespace USING_NS_CC; // create scenario Scene * HelloWorld: createScene () {// create scenario auto scene = Scene: create (); // create layer auto layer = HelloWorld: create (); // Add the layer to the scene-> addChild (layer); // return scene ;} // initialization Layer bool HelloWorld: init () {// initialize the Layer if (! Layer: init () {return false;} // obtain the window Size visibleSize = Director: getInstance ()-> getVisibleSize (); // obtain the coordinate Vec2 origin = Director: getInstance ()-> getVisibleOrigin () for the coordinate origin; // use the image to create a menu item // The first parameter: image in normal state // second parameter: image when selected // third parameter: Response Function auto closeItem = MenuItemImage: create ("CloseNormal.png", "CloseSelected.png ", CC_CALLBACK_1 (HelloWorld: menuCloseCallback, this); // set the location of the menu item closeItem-> setPosition (Vec2 (origin. x + visibleSize. width-closeItem-> getContentSize (). width/2, origin. y + closeItem-> getContentSize (). height/2); // create menu auto Menu = menu: create (closeItem, NULL ); // set the coordinate origin in the menu to the lower left corner (the default coordinate origin in the menu is in the center of the window) menu-> setPosition (Vec2: ZERO ); // Add the menu item to the menu this-> addChild (menu, 1); // create a tag // The first parameter: the content of the tag // The second parameter: font // third parameter: font size auto label = LabelTTF: create ("Hello World", "Arial", 24 ); // set the label position label-> setPosition (Vec2 (origin. x + visibleSize. width/2, origin. y + visibleSize. height-label-> getContentSize (). height); // set the label location this-> addChild (label, 1); // create an sprite auto Sprite = sprite: create ("HelloWorld.png "); // set sprite-> setPosition (Vec2 (visibleSize. width/2 + origin. x, visibleSize. height/2 + origin. y); // Add the genie to the layer. this-> addChild (sprite, 0); return true;} // The Menu response function void HelloWorld :: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = CC_PLATFORM_WP8) | (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) MessageBox ("You pressed the close button. windows Store Apps do not implement a close button. "," Alert "); return; # endif // end scenario ctor: getInstance ()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0 ); # endif}
The CCScene * HelloWorld: scene () in the code in HelloWorldScene. cpp implements the scenario creation process:
1. Create a scenario
2. Create a Layer
3. add layers to scenarios
4. Return scenario
The CCScene * HelloWorld: init () in the code in HelloWorldScene. cpp implements instance initialization:
1. initialize the Layer of the parent class
2. Obtain the window size.
3. Get the coordinates of the window.
4. Create menu items
5. Set the menu item position
6. Set the menu position
7. Add the menu to the Layer
8. Create tags
9. Set the tag location
10. Add tags to Layers
11. Create genie
12. Set the genie location
13. Add the genie to the Layer
Zookeeper
Zookeeper