Cocos2d-x Study Notes (I) HelloCpp

Source: Internet
Author: User

Main. cpp is the entry class of the win32 platform, and AppDelegate is the real entry of the application: The applicationDidFinishLaunching function is used to handle the director class and scenario start and Resource Adaptation: [cpp] bool AppDelegate: applicationDidFinishLaunching () {// initialize director CCDirector * pDirector = CCDirector: sharedDirector (); CCEGLView * pEGLView = CCEGLView: sharedOpenGLView (); pDirector-> setOpenGLView (pEGLView ); // Set the design resolution pEGLView-> setDesignResolutionSize (designResolutionSize. wi Dth, designResolutionSize. height, kResolutionNoBorder); CCSize frameSize = pEGLView-> getFrameSize (); // In this demo, we select resource according to the frame's height. // If the resource size is different from design resolution size, you need to set contentScaleFactor. // We use the ratio of resource's height to the height of design resolution, // this can make sure that the resource's height co Could fit for the height of design resolution. // if the frame's height is larger than the height of medium resource size, select large resource. if (frameSize. height> mediumResource. size. height) {CCFileUtils: sharedFileUtils ()-> setResourceDirectory (largeResource. directory); pDirector-> setContentScaleFactor (largeResource. size. height/designResolutionSize. height);} // if the frame's height is large R than the height of small resource size, select medium resource. else if (frameSize. height> smallResource. size. height) {CCFileUtils: sharedFileUtils ()-> setResourceDirectory (mediumResource. directory); pDirector-> setContentScaleFactor (mediumResource. size. height/designResolutionSize. height);} // if the frame's height is smaller than the height of medium resource size, select small resource. else {CCFileUtils: sharedFileUtils ()-> setResourceDirectory (smallResource. directory); pDirector-> setContentScaleFactor (smallResource. size. height/designResolutionSize. height);} // turn on display FPS pDirector-> setDisplayStats (true); // set FPS. the default value is 1.0/60 if you don't call this pDirector-> setAnimationInterval (1.0/60); // create a scene. it's an autorelease object CCScene * pScene = H ElloWorld: scene (); // run pDirector-> runWithScene (pScene); return true;} The applicationDidEnterBackground () function is called when the program enters the background (inactive, for example, when a phone call is called during a game, the program will enter the background and some resources can be released here, for example, [cpp] void AppDelegate :: applicationDidEnterBackground () {CCDirector: shareddire()-> stopAnimation (); // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine: sharedEngine () -> pauseBackgroundMusic ();} when the program is in When the app is active, the applicationWillEnterForeground () function is called, where you can resume the playing of music, animation, and other [cpp] // this function will be called when the app is active again void AppDelegate :: applicationWillEnterForeground () {CCDirector: sharedDirector ()-> startAnimation (); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine: sharedEngine () -> resumeBackgroundMusic ();} HelloWorldScene scenario class: [cpp] class HelloWorld: public cocos 2d: CCLayer {public: // Here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init (); // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d: CCScene * scene (); // a selector callback void menuCloseCallback (CCObject * pSender); // implement the "static node () "method manually CREATE_FUNC (HelloWo Rld) ;}; the key function is init, which is mainly used to construct various elements in the scenario, such as menu items (CCMenuItem), Genie (CCSprite), and text (CCMenuLabel, after each element is created, you must use the addChild () function to add it to the scene. addChild () has three parameters: 1. the pointer of the object to be added to the scene, 2. Draw the order of layers. 3. Tag. Tag tag is a convenient function. The getChildByTag function retrieves objects for use. The init function of HelloWorld must first call the init () function of the parent class: [cpp] bool HelloWorld :: init () {// 1. super init first if (! CCLayer: init () {return false;} CCSize visibleSize = CCDirector: sharedDirector ()-> getVisibleSize (); CCPoint origin = CCDirector: shareddire () -> 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 exit the progress. it's an autorelpolicobject CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("example", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback )); pCloseItem-> setPosition (ccp (origin. x + visibleSize. width-pCloseItem-> getContentSize (). width/2, origin. y + pCloseItem-> getContentSize (). height/2); // create menu, it's an autorelease object CCMenu * pMenu = CCMenu: create (pCloseItem, NULL); pMenu-> setPosition (CCPointZero ); this-> addChild (pMenu, 1 ); /// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label CCLabelTTF * pLabel = CCLabelTTF: create ("Hello World", "Arial", TITLE_FONT_SIZE ); // position the label on the center of the screen pLabel-> setPosition (ccp (origin. x + visibleSize. width/2, origin. y + visibleSize. height-pLabel-> getContentSize (). height); // add the label as a child to this layer this-> addChild (pLabel, 1); // add "HelloWorld" splash screen "CCSprite * pSprite = CCSprite:: create ("HelloWorld.png"); // position the sprite on the center of the screen pSprite-> setPosition (ccp (visibleSize. width/2 + origin. x, visibleSize. height/2 + origin. y); // add the sprite as a child to this layer this-> addChild (pSprite, 0); return true;} CREATE_FUNC (HelloWorld) is a macro definition equivalent: [cpp] static HelloWorld * create () {HelloWorld * pRet = new HelloWorld (); if (pRet & pRet-> init () {pRet-> autorelease (); // automatically release resources} else {delete pRet; pRet = NULL;} return pRet;} void menuCloseCallback (CCObject * pSender); is a callback function of the menu item, when you click the menu item, this function [cpp] // 2 will be called. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelpolicobject CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("example", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback )); pCloseItem-> setPosition (ccp (origin. x + visibleSize. width-pCloseItem-> getContentSize (). width/2, origin. y + pCloseItem-> getContentSize (). height/2); // create menu, it's an autorelease object CCMenu * pMenu = CCMenu: create (pCloseItem, NULL); pMenu-> setPosition (CCPointZero ); this-> addChild (pMenu, 1); CCScene * HelloWorld: scene (); is to add HelloWorld to the scene: [cpp] CCScene * HelloWorld: scene () {// 'scene' is an autorelease object CCScene * scene = CCScene: create (); // 'player' is an autorelease object HelloWorld * layer = HelloWorld :: create (); // add layer as a child to scene-> addChild (layer); // return the scene return scene ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.