xcodecocos2d-iphonecocos2d-xcocos2d-x-3.0

Detecting tiled map layer collision in cocos2d-x


i am new in cocos2d-x3.2 and i am creating a tile based game something like mario. I created a tile map with a collidable layer . but when I am using Dictionary *properties; then its showing error in dictionary . So what can I use for detecting the collision?

Point tileCoord = this->tileCoordForPosition(position);

int tileGid = CollisionLAyer->tileGIDAt(tileCoord);

if (tileGid) {

    Dictionary *properties;
    properties = _tileMap->propertiesForGID(tileGid);
    if (properties) {
        String *collision = new CCString();
        *collision = *properties->valueForKey("Collidable");
        if (collision && (collision->compare("True") == 0)) {
            return;
        }
    }

Solution

  • With 3.2 I believe the properties are created in a ValueMap.

    Value properties = this->getPropertiesForGID(0);
    if (! properties.isNull())
    {
        ValueMap dict = properties.asValueMap();
        Value collision = dict["Collidable"];
        if (! collision.isNull() && collision.asString() == "True") {
            return;
        }
    }
    

    You can also use an additional tile layer if you want and for example use a red semi-transparent single tile tileset. Draw over all tiles that should be collision tiles. Then you can check if the gid on that layer is > 0 for collisions.