Previous: hhttp://www.cocos2d-x.org/forums/6/topics/51024
6.Collision detected
Since our sprite can move everywhere, you must be very exciting. There is also something strange. Probably you already notice that. The sprite we had made can through everywhere. So we will make it more realistic.
Through the house
Go back to the Tiled Map editor. Press the ‘New Tileset’ menu and open the new material we are going to use. Right-click the red one, and set the new property for it. (Pay attention to the upper and lower case. It may do some trickery.)
Now create a new layer, and name it ‘blockage01’. (Or whatever you like, just remember the name.) Paint those red rectangle on the map to mark the area where our sprite can not move through.
Now, you probably already know what we are going to do! When we are coding we can make the program detect this red rectangle tile and know its property—the ‘Blockage’ is true. (Be wary about whether it’s ‘true’ or ‘True’, it will matter.) Make these area can be through or not.
Return back to the VC. This time I sure you know where to put these exactly.
In the ‘HelloWorldScene.h’:
cocos2d::TMXLayer *_blockage;
In the ‘HelloWorldScene.cpp’:
_blockage = _tileMap->layerNamed("Blockage01");
But this time we will set these red tiles to be invisible.
_blockage->setVisible(false);
Add a new method to calculate the tiles’ coordinate
class HelloWorld : public cocos2d::Layer
{
public:
......
cocos2d::Point tileCoordForPosition(cocos2d::Point position);
......
}
In the ‘HelloWorldScene.cpp’:
Point HelloWorld::tileCoordForPosition(Point position)
{
int x = position.x / _tileMap->getTileSize().width;
int y = ((_tileMap->getMapSize().height * _tileMap->getTileSize().height) - position.y) / _tileMap->getTileSize().height;
return Point(x, y);
}
The original coordinate is on the upper-left corner. So you probably will be confused about the mathematical formula.
The original coordinate
We replace the code in the ‘void HelloWorldPlayerPosition(Point position)’
void HelloWorld::setPlayerPosition(Point position)
{
Point tileCoord = this->tileCoordForPosition(position);
int tileGid = _blockage->getTileGIDAt(tileCoord);
if (tileGid) {
auto properties = _tileMap->getPropertiesForGID(tileGid).asValueMap();
if (!properties.empty()) {
auto collision = properties["Blockage”].asString();
if ("True" == collision) {
return;
}
}
}
_player->setPosition(position);
}
We convert our sprite’s coordinate into the tiles’ coordinate. Then use the method ‘getTileGIDAt’ to get the specified position’s GID. Then use the GID to check the tiles’ property and decide whether it can be go through. (We will add more function in the ‘void HelloWorld::setPlayerPosition(Point position)’ later.)
Try your game. This time the sprite won’t go through the area that you already blockade. And the red tiles won’t be seen since we already set them invisible.
Blockage
Open up a new tile map material and create a new layer in the Tile Map editor. (I name the new layer ‘Foreground01’) And do some farming…on the ‘Foreground01’ layer. Use the green tiles we already met and paint them verdant on the ‘Blockage01’ layer (Yes, the ‘Blockage01’, not the ‘Foreground01’ we created before.)
Set the new property for the green tiles like we did to the red ones.
New property
In the ‘HelloWorldScene.h’:
cocos2d::TMXLayer *_foreground;
In the ‘HelloWorldScene.cpp’:
_foreground = _tileMap->getLayer("Foreground01");
Add these code to the ‘void HelloWorld::setPlayerPosition(Point position)’
auto collectable = properties["Collectable"].asString();
if ("True" == collectable) {
_blockage->removeTileAt(tileCoord);
_foreground->removeTileAt(tileCoord);
_player->setPosition(position);
}
Our sprite is truly an herbivore.
Herbivore
To be continue…
Next: http://www.cocos2d-x.org/forums/6/topics/51363
05.png (144.6 KB)
06.png (7.9 KB)
07.png (220.7 KB)
00.png (158.1 KB)
01.png (14.4 KB)
02.png (547.4 KB)
03.png (54.3 KB)
04.png (493.5 KB)