Cocos2d-x Study Notes (I) HelloWorld
Preface
Formal internship in the company has been in January, before January is mainly to see the "C ++ Primer" to lay the foundation for the study of Cocos2d-x.
I used to develop a simple airplane war with Cocos2d-x at school, I had a simple understanding of the basic controls of Cocos2d-x, this time I hope to learn further and write the form of study notes, be able to have a deeper understanding of Cocos2d-x.
Because the current company uses the Cocos2d-x version is 2.0, so the study is based on version 2.0.
HelloWorld
I remember reading a sneer saying that Chinese people usually like to ask each other if they have eaten it? But programmers won't, because they will ask you today HelloWorld?
I feel like this is always getting hacked on the Internet. Most programmers get used to it, but I still say the importance of HelloWorld to programmers.
It's time to get started during the day. After opening the project, you can see the four files AppDelegate. h/. cpp and HelloWorldScene. h/. cpp, which are a little more complex than the HelloWorld seen by beginners.
# Include AppDelegate. h # include HelloWorldScene. hUSING_NS_CC; AppDelegate: AppDelegate () {} AppDelegate ::~ AppDelegate () {} bool AppDelegate: ccctor () {// initialize CCDirector object CCDirector * pDirector = CCDirector: sharedDirector (); // initialize CCEGLView object, and CCEGLView is the display window, manages and implements window-level functions, including coordinate and zoom management, drawing tools, and key event CCEGLView * pEGLView = CCEGLView: sharedOpenGLView (); // pass the pEGLView to pDirector-> setOpenGLView (pEGLView); // display the open status, including pDirector such as FPS-> setDisplayStats (true); // set the FPS, the number of frames refreshed per second. The default value is 60 frames per second. The higher the number of frames, the smoother the screen, but the more power consumption pDirector-> setAnimationInterval (1.0/60); // create a HelloWorld scenario, the CCScene * pScene = HelloWorld: scene (); // run the HelloWorld scenario pDirector-> runWithScene (pScene); return true ;} // call or the app will call this method when it enters the mobile phone background. void AppDelegate: applicationDidEnterBackground () {// stop all animations CCDirector: sharedDirector ()-> stopAnimation (); // If SimpleAudioEngine (controls background music, etc.) is used, call pause here // SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic ();} // when the application recovers from the background to the foreground, the void AppDelegate: applicationWillEnterForeground () {// restores all animations CCDirector: sharedDirector ()-> startAnimation (); // call SimpleAudioEngine for restoration here // SimpleAudioEngine: sharedEngine ()-> resumeBackgroundMusic ();}
AppDelegate initializes the Cocos2d-x engine and performs global settings.
However, the specific interface implementation is not displayed in this section, because the interface implementation is in HelloWorldScene.
# Include HelloWorldScene. hUSING_NS_CC; CCScene * HelloWorld: scene () {// create a Scene CCScene * scene = CCScene: create (); // create a HelloWorld layer (HelloWorld inherited from CCLayer) helloWorld * layer = HelloWorld: create (); // Add the created HelloWorld layer to scene-> addChild (layer) in the previously created scenario ); // return the created scenario return scene;} // on init you need to initialize your instancebool HelloWorld: init () {// 1. call the initialization of the parent class. If If (! CCLayer: init () {// return false indicates initialization failed return false;} // obtain the size of the visible area CCSize visibleSize = CCDirector: sharedDirector () -> getVisibleSize (); // obtain the CCPoint origin = CCDirector ctor: sharedDirector ()-> getVisibleOrigin (); /// // 2. add a clickable menu button and click it to close the program // create an image menu option CCMenuItemImage * pCloseItem = CCMenuItemImage: create (// call the creation method CloseNormal.png, // set CloseSelected.png ,// Set the menu image this ,//? What is this parameter menu_selector (HelloWorld: menuCloseCallback); // sets the callback listener for the menu Click Time // sets the position coordinate of the menu, pCloseItem-> getContentSize () used to obtain the size of menu options pCloseItem-> setPosition (ccp (origin. x + visibleSize. width-pCloseItem-> getContentSize (). width/2, origin. y + pCloseItem-> getContentSize (). height/2); // create a menu (menu options must be added to the menu to be used), the create FUNCTION can add multiple menu options, add CCMenu * pMenu = CCMenu: create (pCloseItem, NULL) at the end of NULL; // set the coordinate of the menu (CCPointZero is the coordinate (0, 0) pMenu-> setPosition (CCPointZero ); // Add the menu to the HelloWorld layer. 1 indicates the position of the menu on the Z axis in the HelloWorld layer. A greater value indicates a higher level, which is difficult to block this-> addChild (pMenu, 1); // 3. add a text control and a background image // create a file control. The parameters in the create FUNCTION are "Text to be displayed by the control" and "control text font", respectively ", "Control text font size" CCLabelTTF * pLabel = CCLabelTTF: create (Hello World, Arial, 24); // set the position of the file control (the location calculated by this formula is in the center of the screen) pLabel-> setPosition (ccp (origin. x + visibleSize. width/2, origin. y + visibleSize. height-pLabel-> getContentSize (). height); // Add the text control to the HelloWorld layer this-> addChild (pLabel, 1); // create an genie (the specific use of the genie will be introduced later, here, the genie is the carrier of the background image.) CCSprite * pSprite = CCSprite: create(HelloWorld.png); // you can specify the position of the background image. The calculated position is in the center of the screen) pSprite-> setPosition (ccp (visibleSize. width/2 + origin. x, visibleSize. height/2 + origin. y); // Add the background image to the HelloWorld layer, set the Z axis to 0, and place it under the menu and text. this-> addChild (pSprite, 0 ); // return true TO return true;} // The callback function of the close button. The pSender transmits the void HelloWorld: menuCloseCallback (CCObject * pSender) object that calls the function) {// macro definition, determine whether it is a WinRT or WP8 device # if (CC_TARGET_PLATFORM = CC_PLATFORM_WINRT) | (CC_TARGET_PLATFORM = CC_PLATFORM_WP8) // the pop-up dialog box appears, the text message CCMessageBox (You pressed the close button. windows Store Apps do not implement a close button ., alert); # else // call the end () function of CCDirector to end the game CCDirector: sharedDirector ()-> end (); // macro definition, determine whether it is an IOS device # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) // directly call exit (0) to end the game exit (0); # endif}
The HelloWorldScene file is the core of the entire HelloWorld project. From the Code, it is not difficult to find that in the coordinate system calculation of the Cocos2d-x, by default, the lower left corner of the screen is set as the coordinate origin, and the X axis is increased to the upper and right sides. When setting the control position, the control center is the anchor. Of course, the anchorpoint can be changed through code. here we need to call the setAnchorPoint () function.
Well, this chapter is written here, and the next section will continue to learn the overall framework of Cocos2d-x.