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
This article continues with the previous article and continues map collision. in the previous article, we obtained the Map Index of our location, so that we can know the Map Index value corresponding to the location of the role we want to control, so how can we know whether this map can be migrated or collided? Here I will introduce two methods.
1. Hierarchy
I think this is more suitable for a 45-degree map, because if you use the latter method to put all the map width in one layer, it will block this problem, of course, this is not to say that we can't just put the map collision element in a layer, but we will put the corresponding presentation element in a new layer, so we still need to layer it.
First, let's look at the map of the first case, as shown in figure
It is to create a new layer of tree layer, all the trees (which cannot be conflicted here), the grass layer is full of grass, where people can move in the grass, the collision detection code is as follows:
First, use the convertto2d function to obtain the index value of the main character on the map. For convenience, I made the index value the ccpoint type, but in fact it is not a ccpoint, then obtain the tree layer, and then obtain the element at the position based on the index and tileat function. If it is not empty, it indicates that the position cannot be collided, then we can roll back the previous Moving position operation and stop moving the position. This solution is relatively simple.
2. Map GID and Detection Method
This method should be more orthodox. First, create a layer on the map, as shown below:
We only need to create a layer, and then create a new attribute on the graphic element. We need to create a "Conflict" attribute and set different values for it.
We set the value to 1 and the value to 0. The Code is as follows:
First, we can take the GID. In fact, we can directly determine which can be met and which cannot pass through the GID. However, in order to demonstrate how to obtain attributes, we still have the code to get the attributes through gid. The attributes are a set of key-value pairs and then get the conflict value, so that we can determine
If there are any errors, I hope you can correct them more.
Next continue game instance