计算菱形瓦片地图内object的实际瓦片坐标:
//将像素坐标转化为瓦片坐标,偏移坐标以瓦片高度为准,计算瓦片位置需要以object的偏移量计算
getTilePos: function(posInPixel) {
var tileSize = this.theMap.getTileSize()
var cellHeight = tileSize.height;
var cellX = Math.floor(posInPixel.offset.x / cellHeight);
var cellY = Math.floor(posInPixel.offset.y / cellHeight);
return cc.p(cellX, cellY);
},
瓦片坐标转换成实际坐标:
//获取坐标瓦片对应所在 getReleasePos:function (cellX,cellY) { var cellXCount = this.theMap.getMapSize().width; var cellYCount = this.theMap.getMapSize().height; var cellWidth = this.theMap.getTileSize().width; var cellHeight = this.theMap.getTileSize().height; var mapPixWidth = cellWidth * cellXCount; var mapPixHeight = cellHeight * cellYCount; // cellX和cellY是tilemap中的单元格。 var posX = 0 + (cellX - cellY) * cellWidth / 2; var posY = mapPixHeight/2 - (cellX + cellY) * cellHeight / 2; //减去瓦片地图的锚点对应坐标系的位置 return cc.p(posX,posY); },