[Wood Cocos2d-x 010] Game instance-"running" production tutorial (second) -- to join the protagonist

Source: Internet
Author: User

Cocos2d-x game instance-"run and run" production tutorial (article 2) -- add to the protagonist

 

What do you mean? Flowers? No, it's your heart ~

Reprinted please note, original address: http://blog.csdn.net/musicvs/article/details/8188540

 

Body:

 

Note: Please download the resources used in this article: http://download.csdn.net/detail/musicvs/4769412

 

1. entity parent class

Because I think players and monsters have some common operations, so I set both players and monsters as an entity, so we need an entity class as the parent class (create entity under the entity folder. H and entity. CPP file ):

// Entity. hfile # ifndef _ entity_h __# DEFINE _ entity_h __# include "cocos2d. H "using namespace cocos2d; class entity: Public ccnode {public: void setsprite (ccsprite * msprite); protected: ccsprite * msprite;}; # endif

 

// Entity. cpp file # include "entity. H" Void entity: setsprite (ccsprite * msprite) {This-> msprite = msprite ;}

 

The object class is very simple. Currently, there is only one setsprite method.

 

2. Create players

Now we need a player class. Although the Player looks like a genie on the surface, we don't directly let it inherit the genie class. We use a combination to do this. The genie class is just a player attribute. Let the player inherit the entity class (create player. h and player. cpp in the object folder ):

// Player. hfile # ifndef _ player_h __# DEFINE _ player_h __# include "entity. H "class player: public entity {public: create_func (player); Virtual bool Init () ;}; # endif

 

// Player. cpp file # include "Player. H" bool PLAYER: Init () {return true ;}

 

OK. The player has it. Let's add it to the map ~

Open the toll1_scene. cpp file, modify the init method, and add the following code:

/* -------------- Attach player quota */ccsize visiblesize = ccdirector: shareddirector ()-> getvisiblesize (); ccsprite * playersprite = ccsprite: Create ("Sprite/player1.png "); playersprite-> setposition (CCP (100, visiblesize. height/2);/* Add the genie to the map */Map-> addchild (playersprite);/* bind the genie to the Player Object */player * mplayer = PLAYER :: create (); mplayer-> setsprite (playersprite );

 

 

OK, compile and run the program. We will see our main character (whether this character is too ugly =, I can't find the resources that are handsome and running ).

 

3. Fixed gamer birth points (Object layer usage)

Now, the coordinates of the protagonist are written in the code, so we can be more flexible. tiled map also has a very powerful function, that is, the object layer.

In fact, there is not much magic. It is to set an object at a certain position on the map, and then you can get the information of this object in the Code, such as coordinates. By the way, you need coordinates ~

To open our map editor, click the layers in the menu bar, add an Object layer, and create an object Layer:

 

Or click New in the lower-right layer:

 

Then name the layer "objects". Wow, the name of the image ~

 

Now we want to draw an object and click this button on the menu bar:

 

Then draw a rectangle on the map, which represents an object:

 

Next, we will name this object, right-click the rectangle, select the object property, and name it playerpoint:

 

OK. Now we only need to get the playerpoint object in the code and then obtain its X and Y attributes.

Open the tollpolicscene. cpp file, modify the init function, and add the following code before setting the coordinates of the player genie:

/* Load the object Layer */cctmxobjectgroup * objgroup = map-> objectgroupnamed ("objects "); /* load the player coordinate object */ccdictionary * playerpointdic = objgroup-> objectnamed ("playerpoint"); float playerx = playerpointdic-> valueforkey ("X")-> floatvalue (); float playery = playerpointdic-> valueforkey ("Y")-> floatvalue ();/* -------------- load players --------------- */ccsize visiblesize = ccdirector: shareddire () -> getvisiblesize (); ccsprite * playersprite = ccsprite: Create ("Sprite/player1.png"); playersprite-> setposition (CCP (playerx, playery ));

 

 

A brief explanation.

Ctmxobjectgroup stores all objects in our object Layer. Map-> objectgroupnamed ("objects") is to obtain all objects in the obejects Object layer we just created. Of course, currently, there is only one playerpoint object.

How can I obtain a playerpoint object through the objectnamed function? After obtaining the playerpoint object, a ccdictionary object is returned. What is ccdictionary? I wonder if I have used Java? Similar to Java's hashmap, a key value corresponds to a value, and many such key and value pairs can be stored.

Therefore, we can obtain the X and Y coordinates of the playerpoint object from playerpointdic, and then set the coordinates for the player genie.

Come, compile and run, and our leading role will appear in the position we need ~!

 

In the next article, we want to let the main character run. The main character is very handsome ~

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.