2
votes

Hi i m developing one tile map based game in cocos2d. In this i need to find the collision between moving sprite and the layer of the tile map. i followed the raywindlich tutorial. I scaled the tile map in my game. ie. tilemap.scale=0.5;

please help me to find the sprites collision with the map layer.

1

1 Answers

1
votes

This is the function I use to get the tile id for a given layer and a point in screen. It keeps in mind device scale factor.

- (int) getTileGIDMap:(CCTMXTiledMap *) map atLayer:(NSString *) layer andPosition:(CGPoint) position {
    int GID = 0;

    CCTMXLayer *mapLayer1 = [map layerNamed:layer];
    int mapX = position.x * CC_CONTENT_SCALE_FACTOR() / (mapLayer1.mapTileSize.width);
    int mapY = mapLayer1.layerSize.height - (position.y - map.position.y) * CC_CONTENT_SCALE_FACTOR() / mapLayer1.mapTileSize.height;

    if (mapX >= 0 && mapY >= 0 && mapY < map.mapSize.height) {
        GID = [mapLayer1 tileGIDAt:ccp(mapX, mapY)];
    }

    return GID;
}

Hope it helps