Cocos2d-x Study notes (1)

Source: Internet
Author: User

Cocos2d-x prototype cocos2d, based on Cocos2d-iphone, cross-platform.

Hello WORKD Analysis:

1, "Resource" directory

This directory is primarily used to store resource files such as images, audio, and configuration needed in the game. You can create subdirectories in them.

The "resource" directory can be the default game execution time directory.

2. "Include" and "source" directories

These two directories are used to place the game header files and source files. The Main.h, main.cpp, and resource.h that are included in the project template are platform-dependent. Proprietary to Windows.

3, "AppDelegate.h" and "AppDelegate.cpp"

This two file is a generic entry file for cocos2d-x games. Similar to the file where the main function is located in Windowsproject. Open "AppDelegate.cpp" to see the system's own initiative to join the code. Implements the Appdelegate class, which controls the life cycle of the game. With the exception of constructors and destructors, there are three common methods.

BOOL Appdelegate::applicationdidfinishlaunching () {///Initialize director will call this method after launching the application. The default implementation already includes the necessary preparation after the game starts ccdirector* pdirector = Ccdirector::shareddirector ();//Initialize the game engine controller Ccdirector.        To start the game engine cceglview* Peglview = Cceglview::sharedopenglview ();    Pdirector->setopenglview (Peglview);        Turn on display fps enable FPS display pdirector->setdisplaystats (TRUE); Set FPS. The default value is 1.0/60 if you do not call this to set the paint interval.    The human eye has a refresh rate of 1/60 seconds.    Pdirector->setanimationinterval (1.0/60); Create a scene.    It's an Autorelease object creates a scene ccscene *pscene = Helloworld::scene ();    Run execution Scenario Pdirector->runwithscene (pscene); return true;} This function was called when the app was inactive. When comes a phone Call,it's be invoked toovoid Appdelegate::applicationdidenterbackground () {//When the application enters the background. This method is called.

Ccdirector::shareddirector ()->stopanimation (); If you use Simpleaudioengine, it must is pause//Simpleaudioengine::sharedengine ()->pausebackgroundmusic ();} This function would be called time the app is active againvoid Appdelegate::applicationwillenterforeground () {//The method with the previous method Paired, the application is called when it returns to the foreground. Ccdirector::shareddirector ()->startanimation (); If you use the Simpleaudioengine, it must resume here//Simpleaudioengine::sharedengine ()->resumebackgroundmusic ();}

In the first function, you can add Pdirector->enableretinadisplay (true) to open a high-resolution screen.

The default game scenario in the HelloWorld project is defined in HelloWorldScene.h and HelloWorldScene.cpp. Cocos's game structure can be simply summed up as scenes, layers, sprites. The HelloWorld class inherits from Cclayer, so HelloWorld itself is a layer, and the HelloWorld class includes a static function and two instance methods.

Here's a look at:

(1) static cocos2d::ccscene* scene (); is a subclass of Cclayer. The ability to add various sprites and logic processing code to subclasses.

ccscene* Helloworld::scene () {    //' scene ' is an Autorelease object    ccscene *scene = Ccscene::create ();//Create Scene        //' layer ' is an Autorelease object    HelloWorld *layer = Helloworld::create ();//create sub-class layer    //Add layer as a child to scene    Scene->addchild (layer); Add the subclass layer to the Home view    //Return the scene    return scene;
(2) BOOL Init () initializes the HelloWorld class

BOOL Helloworld::init () {////////////////////////////////1. Super Init first initializes the parent class if (!    Cclayer::init ()) {return false; } ccsize visiblesize = Ccdirector::shareddirector ()->getvisiblesize ();//Ccpoint origin = Ccdirector::sharedd Irector ()->getvisibleorigin ();/////////////////////////////////2.    Add a menu item with "X" image, which are clicked to quit the program//may modify it. Create a menu and add to the layer//Add a "close" icon to exit the progress.                                        It ' s an Autorelease object ccmenuitemimage *pcloseitem = Ccmenuitemimage::create ( "Closenormal.png", "closeselected.png", th    IS, Menu_selector (Helloworld::menuclosecallback)); Set the off button position 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 ...//create "Hello World" tab and join to the layer//Add a label shows "Hello World"/Create and initialize a l  Abel cclabelttf* Plabel = cclabelttf::create ("Yu Xikuo", "Arial", 24);//create a label for Yuxikuo//position the label                            On the center of the screen setting label position plabel->setposition (CCP (origin.x + VISIBLESIZE.WIDTH/2,    ORIGIN.Y + visiblesize.height-plabel->getcontentsize (). height));    Add the label as a child to this layer created by the label into the This->addchild (Plabel, 1);    Add "HelloWorld" splash screen "add helloworld.png sprites to the layer ccsprite* Psprite = ccsprite::create (" Helloworld.png "); Position the sprite in the center of the screen set the sprite position in the layer 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); Add sprites to layer return true;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Cocos2d-x Study notes (1)

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.