所谓大型地图,是指类似于pc端沙盒游戏那样的地图。需要加载大量的图片资源。而cocos并不是针对这样的游戏设计(现目前手机端很少有2d的类似游戏)。 笔者最近再开发一款类似《饥荒》的生存类游戏,所以涉及到大型地图的实现,下面来和大家聊一聊我自己的实现方式。 · 首先是底层地图实体的逻辑,要实现物体的碰撞,我使用了方格数组来储存地图的相应信息: struct MapRect { LandType landType_ = DRY_LAND; short int positionX_ = 0; short int positionY_ = 0; }; 底层的数据结构要越简单越好,因为要大量的使用. · 然后是地图上面的实体,在我设计的游戏中有entity和land两种,entity是人为定义的碰撞体积,而land是不规则的图片,我用了像素检测来判断它的边缘,并且存入底层的方格数组中: class VirtualMap { public: VirtualMap(); ~VirtualMap(); public://inlitialize map data bool recordData();//将地图数据存入文件 bool loadData();//from xml;to be done bool loadLandIndex();//from Imgs bool loadPlantIndex();//from plant.xml bool setCache();//set Map sprite cache public://部分成员部分暴露出来供绘制使用 //map data MapRect _landIndex[401][401];//地图网格的数据 SimpleArray<Land*, MAX_LAND_NUMBER> _land; //entities data Vector<Animals*> _ani