[Wood Cocos2d-x 015] Game instance-"Run" production tutorial (article 7)-add items that can eat and victory Conditions

Source: Internet
Author: User

Cocos2d-x game instance-"Run" production tutorial (article 7)-add items that can eat and victory Conditions

 

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

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

 

Body:

 

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

 

 

 

Our games are already very similar to games (like? Is it really not a game ?), So let's add some food for the main character. It's always like this. No pursuit. Hello ~

 

1.
Objects to be painted

Now, we need to open our map editor and draw the following stars as delicious items on the barrier map layer:

 

Let's take a look at my paintings. I still have a master style ~ Bytes

 

2.
Add "alternative" obstacle.

In fact, food items can also be regarded as obstacles, but the obstacles will disappear after the collision with the main character.

As a result, we use the same method as we used to describe the red lattice. However, this time we will describe the blue lattice and select the blue lattice on the graph block. Oh, sorry, it is green =. Right-click the attribute and add an attribute named "star" to the green grid. The attribute value is true.

 

Then, we switch the map to the meta layer and use green grids to cover all our stars ~ The effect is as follows:

 

3.
Let the protagonist eat the stars

In fact, we should have known how to do this for a long time, right? But I am still a little embarrassed.

Open the setsimpleposition function of player. cpp. In the past, we only obtained the colretriable attribute value in the graph lattice of the current location of the main character. This time, we need to obtain another value, that is, the star attribute value. The function is slightly modified, and some fault tolerance mechanisms are added:

Void
PLAYER: setsimpleposition (
Int X, inty)

{

/* ----------------- Determine whether the traffic is not accessible ----------------*/

/* Obtain the grid position of the current protagonist in the map */

Ccpoint
Tiledpos = tilecoordforposition (CCP (x, y ));

 

/* Obtain the unique identifier of the map grid */

Int
Tiledgid = meta-> tilegidat (tiledpos );

 

/* If the value is not 0, the grid exists */

If (tiledgid! = 0 ){

/*

Obtains all attributes of the map lattice. Currently, we only have one colretriable attribute,

The grid belongs to the meta layer, but also belongs to the whole map. Therefore, when obtaining all the properties of the grid,

Obtain the unique identifier of a grid in the map.

*/

Ccdictionary *
Propertiesdict = map-> propertiesforgid (tiledgid );

 

If (propertiesdict! = NULL ){

Const
Ccstring * prop =
Ccstring: Create ("");

 

/* Obtain the colretriable attribute value of the grid and determine whether the colretriable attribute is true. If yes, players are not allowed to move.
*/

Prop =
Propertiesdict-> valueforkey ("colpolicable ");

If (prop-> length ()> 0 & prop-> m_sstring.compare ("true ")
= 0 ){

If (x> 0 ){

X-= 1;

}

Else {

X + = 1;

}

 

If (Y> 0 ){

Y-= 1;

}

Else {

Y + = 1;

}

}

 

/* Get the star attribute value of the grid and determine whether it is true. If yes, the object on the grid will disappear.
*/

Prop =
Propertiesdict-> valueforkey ("star ");

If (prop-> length ()> 0 & prop-> m_sstring.compare ("true ")
= 0 ){

/* Clear the object in the current grid from the obstacle Layer */barrier-> removetileat (tiledpos );

}

}

}

 

Entity: setsimpleposition (x, y );

 

/* Move the map centered on the main character */

Setviewpointbyplayer ();

}

 

The method is the same as that of the previous red lattice. This time, I added a barrier variable, which is our barrier layer and added it to the entity class:

Class
Entity: Public
Ccnode, public
Controllerlistener {

Public:

Void
Setsprite (ccsprite *
Msprite );

Void
Setcontroller (Controller *
Controller );

 

/* Method for implementing the simplemovelistener interface */

Virtual
Void setsimpleposition (intx, inty );

Virtual
Ccpoint getcurposition ();

Protected:

Ccsprite *
Msprite;

Controller *
Mcontroller;

 

/* Map is originally in the player class. Now we put it here */

Cctmxtiledmap *
Map;

 

/* Map layer for collision detection */

Cctmxlayer * Meta;

 

/* Obstacle Layer */

Cctmxlayer *
Barrier;

 

/* Convert pixel coordinates to map grid coordinates */

Ccpoint
Tilecoordforposition (ccpoint
Pos );

};

 

 

Then assign the barrier value to the initwithtiledmap function of player. cpp:

/* Load all objects in the object Layer */

Cctmxobjectgroup *
Objgroup = map-> objectgroupnamed ("objects ");

 

/* Load the meta Layer */

Meta =
Map-> layernamed ("meta ");

Meta-> setvisible (false );

 

/* Load the obstacle Layer */

Barrier =
Map-> layernamed ("barrier ");

 

 

OK. Compile and run it. When the protagonist passes through the stars, it will eat it. However, it just eats some of it. This is not very good, but for the moment.

 

By the way, have we found that the main character is under the stars and stones? This is a layer order problem. Let's just make a small change. Open the initwithtiledmap function of player. cpp and add an order parameter when adding the main character genie to the map:

/* Add the genie to map hosts? */

Map-> addchild (playersprite, 20 );

 

In this way, the main character will not be listed below.

 

4.
Add victory Conditions

We can still use the red and green grids to add a purple lattice and draw a vertical line at the rightmost of the map, then add a property "win" to the purple lattice and the value is true. Then, when the protagonist enters the position of these purple grids on the map, it is a victory.

However, due to the time relationship, this tutorial took me a lot of time and I will not detail it.

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.