Cocos2d x to learn the HelloWorld program

  • 2020-04-02 02:41:02
  • OfStack

I. introduction:

Cocos2d-x is a very popular open source mobile 2D game framework. The cocos2d-x version used in this article's HelloWorld sample program is 2.0, which mainly implements a simple entry program.

Ii. HelloWorld program

The HelloWorld program is an introduction to many programming languages and is very important to programmers.
After opening the project of this article, you can see the four files of AppDelegate. H /. CPP and HelloWorldScene. H /. CPP.

The specific code is as follows:


#include "AppDelegate.h" 
#include "HelloWorldScene.h" 
USING_NS_CC; 
AppDelegate::AppDelegate() { 
} 
AppDelegate::~AppDelegate()  
{ 
} 
bool AppDelegate::applicationDidFinishLaunching() { 
  //Initializes the CCDirector object
  CCDirector* pDirector = CCDirector::sharedDirector(); 
  //Initializes the CCEGLView object, which is the display window, and is responsible for the management and implementation of window-level functions, including coordinate and zoom management, drawing tools, and key events
  CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); 
  //Pass pEGLView to pDirector
  pDirector->setOpenGLView(pEGLView); 
  //Open status display, including FPS, etc
  pDirector->setDisplayStats(true); 
  //Set FPS to refresh the number of frames per second, the default is 60 frames per second, the higher the number of frames, the smoother the picture, but also the more power consumption
  pDirector->setAnimationInterval(1.0 / 60); 
  //Create a HelloWorld scene that can be automatically released
  CCScene *pScene = HelloWorld::scene(); 
  //Run the HelloWorld scenario
  pDirector->runWithScene(pScene); 
  return true; 
} 
//This method is called when an incoming call or application enters the background of the phone
void AppDelegate::applicationDidEnterBackground() { 
  //Stop all animation
  CCDirector::sharedDirector()->stopAnimation(); 
  //If you are using SimpleAudioEngine (controlling background music, etc.), pause is called here
  // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); 
} 
//This method is called when the application is restored from the background to the foreground
void AppDelegate::applicationWillEnterForeground() { 
  //Restore all animations
  CCDirector::sharedDirector()->startAnimation(); 
  //Call the restore of SimpleAudioEngine here
  // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); 
} 

The AppDelegate initializes the cocos2d-x engine and makes some global Settings for it.
But you don't see the interface implementation in this case, because the interface implementation is in the HelloWorldScene.


#include "HelloWorldScene.h" 
USING_NS_CC; 
CCScene* HelloWorld::scene() 
{ 
  //Create a Scene
  CCScene *scene = CCScene::create(); 
   
  //Create a layer for HelloWorld (HelloWorld inherited from CCLayer)
  HelloWorld *layer = HelloWorld::create(); 
 
  //Add the created HelloWorld layer to the scene you created earlier
  scene->addChild(layer); 
 
  //Returns the created scenario
  return scene; 
} 
 
// on "init" you need to initialize your instance 
bool HelloWorld::init() 
{ 
  ///////////////////////////// 
  //1. Call the initialization of the parent class. If the initialization fails, the execution will not continue
  if ( !CCLayer::init() ) 
  { 
    //Returning false indicates an initialization failure
    return false; 
  } 
  //Gets the size of the displayable area
  CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); 
  //Gets the starting point of the coordinates of the displayable region
  CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); 
  ///////////////////////////// 
  //2. Add a clickable menu button, click to close the program
  //Create an image menu option
  CCMenuItemImage *pCloseItem = CCMenuItemImage::create(//Calling the create method
                    "CloseNormal.png",//Set the menu picture when not clicked
                    "CloseSelected.png",//Set menu picture when click
                    this,//The & # 63; What is this parameter
                    menu_selector(HelloWorld::menuCloseCallback));//Set menu click time callback monitor
  //Set the position coordinates of the menu, pCloseItem-> GetContentSize () is used to get the menu option size
  pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , 
                origin.y + pCloseItem->getContentSize().height/2)); 
  //To create a menu (menu options need to be added to the menu to be used), the create function can add more than one menu option, ending with NULL
  CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); 
  //Set the coordinates of the menu (CCPointZero is the coordinates (0,0))
  pMenu->setPosition(CCPointZero); 
  //Add the menu to the HelloWorld layer. 1 is the z-axis position of the menu on the HelloWorld layer
  this->addChild(pMenu, 1); 
  ///////////////////////////// 
  //3. Add text controls and background pictures
  //Create a file control. The parameters in the create function are "text to be displayed by the control", "font of the control text", and "font size of the control text".
  CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); 
  //Set the file control position (this formula calculates the position as the center of the screen)
  pLabel->setPosition(ccp(origin.x + visibleSize.width/2, 
              origin.y + visibleSize.height - pLabel->getContentSize().height)); 
  //Adds a text control to the HelloWorld layer
  this->addChild(pLabel, 1); 
  //Create a Sprite (more on how sprites are useful, where sprites are the carrier of the background image)
  CCSprite* pSprite = CCSprite::create("HelloWorld.png"); 
 
  //Set the position of the background picture (the position calculated by this formula is 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 and set the z-axis to 0, under the menu and text
  this->addChild(pSprite, 0); 
  //Returning true indicates successful initialization
  return true; 
} 
//Close the callback function of the button, and the pSender passes the object that called the function
void HelloWorld::menuCloseCallback(CCObject* pSender) 
{ 
//Macro definition to determine whether it is a WinRT or WP8 device
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) 
  //Pop-up dialog box, prompt text message
  CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); 
#else 
  //Call CCDirector's end() function to end the game
  CCDirector::sharedDirector()->end(); 
//Macro definition to determine if it is an IOS device
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 
  //Call exit(0) directly to end the game
  exit(0); 
#endif 
#endif 
} 

HelloWorldScene file is the core of the whole HelloWorld project. It is not difficult to find from the code that in the calculation of cocos2d-x coordinate system, the left corner of the screen is set as the origin of coordinates by default, and the Y axis x axis is increased to the top and right. When setting the position of the control, the center of the control is the anchor point, of course, the anchor point can be changed through the code, here we need to call the setAnchorPoint() function.

I hope the examples described in this paper can help you to learn cocos2d-x.


Related articles: