What programs are started from HelloWorld first. The same cocos2d-x we start from the HelloWorld, the following is the completion of the HelloWorld operation diagram:
There will be two more commonly used folders in the Cocos game project. They are classes and resource respectively. Classes access code file, resource access the resource file, the following is the complete project schema:
Let's first look at the basic AppDelegate.cpp class.
#include "AppDelegate.h" #include "HelloWorldScene.h"//namespace USING_NS_CC; Constructor Appdelegate::appdelegate () {}//destructor Appdelegate::~appdelegate () {}//The function that will enter after the program starts is appdelegate:: Applicationdidfinishlaunching () {//Initialize directed auto director = Director::getinstance (); Get opengl View Auto Glview = Director->getopenglview (); If you do not get OpenGL view if (!glview) {//create OpenGL View Glview = Glview::create ("My Game"); Set OpenGL view Director->setopenglview (Glview); }//sets whether to display debug information Director->setdisplaystats (TRUE); Set frame rate director->setanimationinterval (1.0/60); Call scene Auto scene = Helloworld::createscene (); Execution Scenario Director->runwithscene (scene); return true;} The function that is called when the program enters the background (when someone calls when the game is played, the program enters the background) void Appdelegate::applicationdidenterbackground () {//stop playing the animation Director:: GetInstance ()->stopanimation (); Pause playing background music//simpleaudioengine::getinstance ()->pausebackgroundmusic ();} When the program re-function called when activated (sound re-sounded) void Appdelegate::applicationwillenterforeground () {//Play Animation director::getinstance ()->st Artanimation (); Continue playing background music//simpleaudioengine::getinstance ()->resumebackgroundmusic ();}
HelloWorld.h File
#ifndef __helloworld_scene_h__#define __helloworld_scene_h__ #include "cocos2d.h"//helloworld class inherits from the Layer class Helloworld:public cocos2d::layer{public: //create scene static cocos2d::scene* Createscene (); Initialization layer virtual BOOL init (); Menu response function void Menuclosecallback (cocos2d::ref* psender); Used to create: scenes, menus, layers, and more.
The scene* helloworld::scene () in the code in HelloWorldScene.cpp implements the process of creating the scene:
1. Create a scene
2. Create Layer
3. Add layers to the scene
4. Return to the scene
HelloWorld.cpp
#include "HelloWorldScene.h"//namespace USING_NS_CC; Create Scene scene* Helloworld::createscene () {//create scene Auto scene = Scene::create (); Create layers Auto layer = helloworld::create (); Add a layer to the scene scene->addchild (layer); Return to the scene return scenes;} Initialize layer bool Helloworld::init () {//Initialize the parent class's layer if (! Layer::init ()) {return false; }//Gets the size of the window Visiblesize = director::getinstance ()->getvisiblesize (); Get coordinates of origin of coordinates Vec2 origin = Director::getinstance ()->getvisibleorigin (); Create a menu item with a picture//first parameter: Picture in normal state//second parameter: Picture when selected//third parameter: Response function Auto Closeitem = Menuitemimage::create ( "Closenormal.png", "Closeselected.png", Cc_callback_1 (Helloworld::menuclosecallback, this)); Sets the location of the menu item closeitem->setposition (VEC2 (origin.x + visiblesize.width-closeitem->getcontentsize (). WIDTH/2, ORIGIN.Y + closeitem->getcontentsize (). HEIGHT/2)); Create menus Auto menu = Menu::create (Closeitem, NULL); Set the coordinate origin of the menu to the lower-left corner (the default coordinate origin in the menu in the center of the window) menu->setposition (Vec2::zero); Add menu items to menus this->addchild (menu, 1); Create a label//first parameter: The contents of the tag//The second parameter: Font//third parameter: Font size auto label = labelttf::create ("Hello World", "Arial", 24); Sets the position of the label Label->setposition (VEC2 (origin.x + VISIBLESIZE.WIDTH/2, ORIGIN.Y + visiblesize. Height-label->getcontentsize (). height)); Sets the position of the label This->addchild (label, 1); Create a Sprite Auto Sprite = sprite::create ("Helloworld.png"); Set the location of the sprite Sprite->setposition (VEC2 (VISIBLESIZE.WIDTH/2 + origin.x, VISIBLESIZE.HEIGHT/2 + origin.y)); Add sprites to the layer This->addchild (sprite, 0); return true;} Menu response function void Helloworld::menuclosecallback (ref* psender) {#if (Cc_target_platform = = CC_PLATFORM_WP8) | | (Cc_target_platform = = Cc_platform_WINRT) MessageBox ("You pressed the Close button. Windows Store Apps do not implement a close button. "," Alert "); return; #endif//End Scene director::getinstance ()->end (); #if (Cc_target_platform = = Cc_platform_ios) exit (0); #endif}
HelloWorldScene.cpp in the Helloworld::init (), implemented the initialization:
1. Initialize the layer of the parent class
2, get the size of the window
3, get the coordinates of the window
4. Create a menu item
5. Set the location of the menu item
6. Setting the location of the menu
7. Add the menu to the layer
8. Create a Label
9, set the location of the label
10. Add the label to the layer
11. Creating Sprites
12, set the location of the wizard
13. Add Sprites to the layer
#include "main.h" #include "AppDelegate.h" #include "cocos2d.h"//namespace USING_NS_CC; The main function of the cocos2d-x (equivalent to the main function in C + +) is int apientry _twinmain (hinstance hinstance, hinstance hprevinstance, LPTSTR lpcmdline, int ncmdshow) { //= lpCmdLine, nCmdShow is two useless parameters Unreferenced_ PARAMETER (hprevinstance); Unreferenced_parameter (lpcmdline); Define an App object appdelegate app; Executes the run function of the App object. Enter the frame loop return application::getinstance ()->run ();}
The code in Main.cpp only implements the following actions
To define an App object, execute the App object into the frame loop
A game program to do so, came into being, is not feeling particularly magical. OK about the HelloWorld of the analysis to share this. Next chapter into the menu of learning
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Cocos2d Introductory Tutorial three" HelloWorld at a Glance