Cocos2d-x learning journey (9): 2.2 pangu, enter the game world

Source: Internet
Author: User

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.

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.