In the chaotic world, pangu cannot bear the loneliness. Instead, it breaks down its teeth and turns into a huge axe to open the world. It creates everything in the universe and evolves into a vibrant world. Let's assume pangu, create a new game world.
In the process of game development, we need to understand several important concepts: Directors, scenes, sets, roles, and actions. Since we play pangu, we are responsible for the evolution of Heaven and Earth.
1. ccdirector, setting and action in order
2. ccscene (heaven and earth): pangu uses its body to open the world. The so-called Heaven and Earth are our common checkpoints in the game. The levels are mainly composed of scenarios and roles.
3. ccamera. In the game, different angles, rotation, location changes, scaling.
4. cclayer: Before pangu's death, the breath in his mouth turned into the clouds of spring breeze and the sky; the muscles turned into the fertile mountains of miles for the life of all kinds
Save. In the game, the set is the background in each scenario, namely mountains, Earth, and sky.
5. ccsprite (all beings): All beings live on the earth, persist and multiply, evolve and kill. In the game, the characters walk, the e soar, And the monsters run rampant. These can be created by yourself.
6. ccaction: the birth of humans and the killing of prey. In the game, to survive and fight against monsters, you have to possess various skills and actions, which requires the support of ccaction.
Step 1: use the resource file ccsprite1_start.jpg as the background of the game in the initworldof helloworld..
// load the scene ccsize size = ccdirector: shareddirector ()-> getwinsize (); ccsprite * psprite = ccsprite: Create ( "start.jpg" ); cc_break_if (! Psprite); psprite-> setposition (CCP (size. width/2, size. height/2); // Add the sprite to helloworld layer as a child layer. This -> addchild (psprite, 0);
There is only one director in the game, so ccdireuses Singleton mode, why is Singleton mode used, and its benefits? I will talk about it separately in the following sections.
Ccsprite initialization to load resources using the create method, this is used since the cocos2d-x2.0, the use of autorelock Automatic Management of memory, so when we make
You do not need to manually release the memory
Step 2: load the control menu of the game
Add source file in Resoure. h
Static Const CharS_playfont [] ="Fonts/bitmapfontchinese. fnt";Static Const CharS_start [] ="Start game";Static Const CharS_exit [] ="Exit game";
Load the menu after loading the screen
// Load the game menuCclabelbmfont * startfontstyle = cclabelbmfont: Create (s_start, s_playfont); ccmenuitemlabel * startitem = ccmenuitemlabel: Create (startfontstyle,This, Menu_selector (helloworld: menuplaynewgame); optional * exitfontstyle = require: Create (s_exit, s_playfont); ccmenuitemlabel * exititem = ccmenuitemlabel: Create (exitfontstyle,This, Menu_selector (helloworld: menuclosecallback); ccmenu * startmenu = ccmenu: Create (startitem, exititem, null); startmenu-> alignitemsvertically ();This-> Addchild (startmenu );
Game menu item:
Ccmenuitemsprite: uses image sprite as the menu item of the menu.
Cclabelatlas: After the font file is set and the initial characters of the font are defined in ASCII order, you can use the font to create a text label.
Ccmenuitemlabel: A menu item created in label mode. It is automatically processed by clicking or enlarging it as the menu operation result.
Ccmenuitemfont: common text menu item
Cclabelbmfont: Text menu item in font
Add the menu callback method declaration to helloworld. h.
// A selector callbackVoidMenuclosecallback (ccobject * psender );VoidMenuplaynewgame (ccobject * sender );
Add the menu callback method to helloworld. cpp.
VoidHelloworld: menuclosecallback (ccobject * psender ){// "Close" menu item clickedCcdirector: shareddirector ()-> end ();}VoidHelloworld: menuplaynewgame (ccobject * sender ){}
The next chapter begins to create a tmx game map and load it into our new scenario.