转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/24414375
源码下载:http://download.csdn.net/detail/oyangyufu/7272177
地图布局效果图:
源码分析:
//MapLayer场景数据、UI初始化
bool MapLayer::init() { int w=0; if (wSize.width==1136 && wSize.height==640) { w=497*2-40; } else { w=410*2-40; } if ( !CCLayerColor::initWithColor(ccc4(0, 0, 0, 150),w,260*2)) { return false; } this->initData(); this->initUI(); return true; }
读取levelinfo.plist文件数据,缓存icon图片数据, 添加touch监听
void MapLayer::initData()
{
countCleared = 0;
//读取plist数据文件
const char *plistPath=CCFileUtils::sharedFileUtils()->fullPathFromRelativeFile("levelinfo.plist","levelinfo.plist");
CCDictionary *plistDic=CCDictionary::createWithContentsOfFile(plistPath);
CCDictionary *levelDic=dynamic_cast<CCDictionary *>(plistDic->objectForKey("001"));
CCString *xstring=dynamic_cast<CCString *>(levelDic->objectForKey("total_x")); //获取地图部署列数
total_x=xstring->intValue();
CCString *ystring=dynamic_cast<CCString *>(levelDic->objectForKey("total_y")); //获取地图部署行数
total_y=ystring->intValue();
CCArray *array=CCArray::create();
array=dynamic_cast<CCArray *>(levelDic->objectForKey("imageidarr")); //获取imageidarr标签值存入数组
mapArray=CCArray::create();
mapArray->retain();
CCArray *array2=CCArray::create();
srand((unsigned int)time(NULL));
for (int i=0; i<(total_x-1)*(total_y-1); i++)//将arary数组数据取出来赋值给node再存入array2
{
MapNode *node=new MapNode();
node->autorelease();
//产生唯一的orderid
node->order=(int)(CCRANDOM_0_1()*INT_MAX)%(int)(CCRANDOM_0_1()*INT_MAX); //生成唯一序号
CCString *idString=(CCString *)(array->objectAtIndex(i)); //根据从数组获取imageidarr标签值
CCLOG("========== i: %d, idString: %s", i, idString->getCString());
node->imgid=idString->intValue();//那些标签实际就当作图片id
array2->addObject(node);
}
//排序
qsort(array2->data->arr, array2->data->num, sizeof(long),compare);//从大到小排序
for (int x=0; x<total_x; x++) //地图列
{
for (int y=0; y<total_y; y++) //地图行
{
if (x==0 || y==0)
{
MapNode *node=new MapNode();
node->autorelease();
node->order=0;
node->imgid=0;
mapArray->addObject(node);
}else
{
int i = (y - 1) * (total_x - 1) + x - 1;
mapArray->addObject(array2->objectAtIndex(i));
CCLOG("i: %d", i);
}
}
}
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("icon1.plist");
//添加touch监听
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 1, true); //添加触摸事件
prePoint = CCPointMake(-1, -1); //初始化前一个触摸坐标
}
布局UI,5x9个格子,13对图标:
void MapLayer::initUI() { //布局地图精灵 for (int y = 0; y < total_y; ++y) //行 { for (int x = 0; x < total_x; ++x) //列 { int index=y*total_x+x; if (this->imageFilename(index)) { CCSprite *sprite=CCSprite::createWithSpriteFrameName(this->imageFilename(index)->getCString()); //CCLOG("imagefilename: %s, x: %f, y: %f", this->imageFilename(index)->getCString(), OFFSET_X * x + (SIZE_W / 2) + SIZE_W * x, OFFSET_Y * y + (SIZE_H / 2) + SIZE_H * y); sprite->setScale(1.0); sprite->setPosition(ccp(OFFSET_X * x + (SIZE_W / 2) + SIZE_W * x, OFFSET_Y * y + (SIZE_H / 2) + SIZE_H * y)); this->addChild(sprite, 1, TAG_START_SPRITE+index); } } } }