Comment directly without talking nonsense
<span style="font-size:24px;color:#ff0000;">AppDelegate.cpp</span>
# Include "appdelegate. H "# include" helloworldscene. H "// using_ns_cc = Using namespace cocos2dusing_ns_cc; // using namespace cocos2d // constructor appdelegate: appdelegate () {}// destructor appdelegate ::~ Appdelegate () {}// program initialization function bool appdelegate: applicationdidfinishlaunching () {// initialize Director // get the device auto Director ctor = Director ::: getinstance (); // obtain the OpenGL window auto glview = Director-> getopenglview (); If (! Glview) {// if it is null, a window with the title "www.sundabke.com" is created. Glview = glview: Create ("www.sundabke.com "); // here I have modified // my game at the beginning. I changed it to www.sundabke.com cocos2dx. The default window size is 960,640. // you can delete this sentence. // Director-> setopenglview (glview); this sentence is written below my personal medical insurance} // set the window for device use, Director-> setopenglview (glview ); // turn on display FPS // enable FPS display ctor-> setdisplaystats (true); // set FPS. the default value is 1.0/60 if you don't call this // set 60 frames per second Director-> setanimationinterval (1.0/60); // create a scene. it's an autorelease object // create helloworld scenario auto scene = helloworld: createscene (); // run scenario Director-> runwithscene (scene); Return true ;} // this function will be called when the app is inactive. when comes a phone call, it's be invoked too // when receiving the call, the game is transferred to the background service. In response, void appdelegate: applicationdidenterbackground () {Director :: getinstance ()-> stopanimation (); // If used, use this code to pause. // if you use simpleaudioengine, it must be pause // simpleaudioengine :: getinstance ()-> pausebackgroundmusic ();} // this function will be called when the app is active again // when the call is complete, choose resume game yes, response to this void appdelegate: applicationwillenterforeground () {Director: getinstance ()-> startanimation (); // if you use simpleaudioengine, it must resume here // if the sound is used, you can use this code to restore it. // simpleaudioengine: getinstance ()-> resumebackgroundmusic ();}
<span style="font-size:48px;color:#ff0000;"></span><pre name="code" class="cpp"><span style="font-size:48px;color:#ff6666;">HelloWorldScene.h</span>
<PRE name = "code" class = "CPP"> # ifndef _ helloworld_scene_h __# DEFINE _ helloworld_scene_h __# include "cocos2d. H "class helloworld: Public cocos2d: layer {public: // There's no 'id' in CPP, so we recommend returning the class instance pointer // create a static function createscene cocos2dx3. X is the static cocos2d: Scene * createscene (); // here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning' I D' in cocos2d-iphone // initialize virtual bool Init (); // a selector callback // response button exit program void menuclosecallback (cocos2d: ref * psender ); // implement the "static create ()" method manually // Add a static create function to create an instance. Create_func (helloworld) ;}; # endif // _ helloworld_scene_h __
Helloworldscene. cpp
# Include "helloworldscene. H "using_ns_cc; // use the cocos2dx namespace using_ns_cc = Using namespace cocos2d // static function creation scenario SCENE * helloworld: createscene () {// 'Scene 'is an autorelease object <span style = "white-space: pre"> </span> // create a scene auto scene = scene :: create (); // 'player' is an autorelease object <span style = "white-space: pre"> </span> // create a Layer Auto layer = helloworld :: create (); // Add layer as a child to S Cene <span style = "white-space: pre"> </span> // send the layer to scene-> addchild (Layer ); // return the scene return scene;} // on "init" you need to initialize your instancebool helloworld: Init () {// 1. super init first <span style = "white-space: pre"> </span> // initialize if (! Layer: Init () {return false;} // obtain the screen resolution size visiblesize = Director: getinstance ()-> getvisiblesize (); <span style = "white-space: pre"> </span> <span style = "white-space: pre "> </span> // obtain the dot coordinate vec2 origin = Director: getinstance ()-> getvisibleorigin (); /// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // Add a "close" icon to ex It the progress. it's an autorelease object <span style = "white-space: pre"> </span> // create a button, which is represented by two images. By default, it is the first image, press the second image and call the helloworld: menuclosecallback method auto closeitem = menuitemimage: Create ("closenormal.png", "closeselected.png", cc_callback_1 (helloworld: menuclosecallback, this); // set the button position <span style = "white-space: pre"> </span> closeitem-> setposition (vec2 (origin. X + visiblesize. width-closeitem-> Getcontentsize (). width/2, origin. Y + closeitem-> getcontentsize (). height/2); // create menu, it's an autorelease object <span style = "white-space: pre "> </span> // The button created by the menu item. auto menu = menu: Create (closeitem, null); menu-> setposition (vec2: zero); // set the menu position this-> addchild (menu, 1 ); /// // 3. add your codes below... // Add a label shows "Hello World" // create and initialize Label // create a text label, auto label = labelttf: Create ("www.sundabke.com", "Arial", 24 ); // position the label on the center of the screen <span style = "white-space: pre "> </span> // set the location label-> setposition (vec2 (origin. X + visiblesize. width/2, origin. Y + visiblesize. height-label-> getcontentsize (). height); // Add the label as a child to this layer this-> addchild (Label, 1); // display the label on the screen // Add "helloworld "Splash screen" <span style = "white-space: pre"> </span> // create a background auto sprite = sprite: Create ("helloworld.png "); // position the Sprite on the center of the screen <span style = "white-space: pre "> </span> // set the position of the background sprite-> setposition (vec2 (visiblesize. width/2 + origin. x, visiblesize. height/2 + origin. y); // Add the sprite as a child to this layer <span style = "white-space: pre "> </span> // Add the background to the screen. This-> ADDC Hild (sprite, 0); Return true;} // event processing when the response button is pressed void helloworld: menuclosecallback (ref * psender) {<span style = "white-space: pre "> </span> // if it is a WP8 platform, a message box is displayed. # If (cc_target_platform = cc_platform_wp8) | (cc_target_platform = cc_platform_winrt) <span style = "white-space: pre "> </span> MessageBox (" you pressed the close button. windows Store apps do not implement a close button. "," alert "); return; # endif <span style =" white-space: pre "> </span> // otherwise, terminate the program. Director: getinstance ()-> end (); <span style = "white-space: pre"> </span> // exit the Program # If (cc_target_platform = cc_platform_ios) exit (0); # endif}
Cocos2dx3.2helloword Analysis