Small man (Bill man) Personal Original, welcome to reprint, reprint please indicate the address, small man (Bill man) column address http://blog.csdn.net/bill_man
Continue with the previous action game instance and add the collision between left and right borders. The effect is as follows:
The collision between left and right borders requires the help of the graphic attribute section in the map
Right-click the attribute, add the collion attribute (custom name), and set the attribute value to 2, in this way, after obtaining the attribute in our program, we can determine whether this graphic element is a collision block in the map. To obtain the attribute of the map, we mainly use the following statements:
Cctmxlayer * layer = map-> layernamed ("logic"); // obtain the corresponding map layer
Int tilegid = Layer-> tilegidat (playerindex); // obtain the GID
Ccmutabledictionary <string, ccstring *> * tiledic = map-> propertiesforgid (tilegid); // obtain the attribute key-Value Pair
Ccstring * mvalue = tiledic-> objectforkey ("collion"); // obtain the attribute colloion
Int mv = mvalue-> toint (); // convert to an integer value
The overall judgment level collision function playermaphcollision is as follows:
The first is to obtain the Standing Position of the main character, the width and height of the main character, and then obtain the map object, which is a for loop, which traverses the base of the main character to the ordinate value above the head, then, the corresponding ordinate value and abscissa value are divided by the width and height of the Graphic Element to obtain the index coordinates of the corresponding map. Because we want to detect the collision between the left and the right, the corresponding abscissa is different.
Obtain the horizontal coordinate on the left: indexx = (playerpoint. X-palyersize. width/2)/Map-> gettilesize (). width;
Obtain the horizontal coordinate on the right: indexx = (playerpoint. x + palyersize. width/2-1)/Map-> gettilesize (). width;
Because the anchor is in the center, the left and right sides are the minus and the half of the plus width. The difference is that we need to subtract one when processing the right side, because the collision on the left side gets the left side of the boundary, the collision on the right side gets the right side of the boundary. When our coordinates belong to the following range, it belongs to the index of this graphic block: (index-1) * graphic width <= x <Index * graphic width, that is, if our coordinates are equal to the right boundary value, they will be included in the next index value, therefore, when processing the right side, in order to avoid the problem that the map is stuck when the next block is accidentally included in the boundary value, you can subtract one from the collision on the right side.
The following describes how to set the horizontal movement flag to 0 when a collision block is hit, correct the position, move the protagonist to the non-collision position, and reset the protagonist position;
In addition, to solve the problem in the previous article, we need to obtain the absolute coordinates of the main character when getting the position of the main character on the screen. We need to add the coordinates of the map, because when we set the position of the main character, is set on the map, so the protagonist's position is relative to the map coordinate, the Code is as follows:
If there are any errors, I hope you can correct them more.
Next continue simple action game