CCSprite* CMGameMap::TileMapLayerPosToTileSprite( CCPoint TileMapLayerPos) { do { CCPoint TileMapPos = TileMapLayerPosToTileMapPos(TileMapLayerPos); //获得地图的各个层 CCTMXLayer* pCloudLayer = layerNamed("cloud"); CC_BREAK_IF(pCloudLayer==NULL); CCTMXLayer* pBlockLayer = layerNamed("block"); CC_BREAK_IF(pBlockLayer==NULL); CCTMXLayer* pPipeLayer = layerNamed("pipe"); CC_BREAK_IF(pPipeLayer==NULL); CCTMXLayer* pLandLayer = layerNamed("land"); CC_BREAK_IF(pLandLayer==NULL); CCTMXLayer* pTrapLayer = layerNamed("trap"); CC_BREAK_IF(pTrapLayer==NULL); CCTMXObjectGroup* pObjectLayer = objectGroupNamed("objects"); CC_BREAK_IF(pObjectLayer==NULL); CCTMXLayer* pCoinLayer = layerNamed("coin"); CC_BREAK_IF(pCoinLayer==NULL); CCTMXLayer* pFlagpoleLayer = layerNamed("flagpole"); CC_BREAK_IF(pFlagpoleLayer==NULL); //若马里奥超过上边界 if(TileMapPos.y<0)return NULL; CCSprite* pLandSprite = pLandLayer->tileAt(ccp(TileMapPos.x,TileMapPos.y)); if (pLandSprite!=NULL) { return pLandSprite; } CCSprite* pBlockSprite = pBlockLayer->tileAt(ccp(TileMapPos.x,TileMapPos.y)); if (pBlockSprite!=NULL) { //遍历砖块数组,如果砖块数组中未找到,则说明已被顶坏,返回空 CCObject *pObj = NULL; CCARRAY_FOREACH(m_pArrayBlocks,pObj) { CMItemBlock* pItem = dynamic_cast<CMItemBlock*>(pObj); CC_BREAK_IF(pItem==NULL); CCPoint CurBlockWorldPos = (pBlockSprite->getPosition()); CCPoint TempBlockWorldPos = (pItem->getPosition()); //找到则返回砖块精灵 if (abs(CurBlockWorldPos.x==TempBlockWorldPos.x) && abs(CurBlockWorldPos.y==TempBlockWorldPos.y)) { return pBlockSprite; } } return NULL; }
// XXX deprecated CCTMXObjectGroup * CCTMXTiledMap::groupNamed(const char *groupName) { return objectGroupNamed(groupName); }
bool CMGameMap::Init(CMReceiver* pReceiver,enumMarioLevel &eMarioLevel) { do { //初始化成员变量 m_fDropSpeedPlus = 0; m_fJumpSpeed = 0; m_bIsLeftKeyDown = false; m_bIsRightKeyDown = false; m_bIsJumpKeyDown = false; m_bIsFireKeyDown = false; m_bIsHeroDead = false; m_pReceiver = pReceiver; m_bNeedResetStage = false; //初始化游戏对象数组 m_pArrayItems = CCArray::create(); m_pArrayItems->retain(); m_pArrayMonsters = CCArray::create(); m_pArrayMonsters->retain(); m_pArrayBlocks = CCArray::create(); m_pArrayBlocks->retain(); m_pArrayFireBall = CCArray::create(); m_pArrayFireBall->retain(); //初始化Mario CMMario* pMario = CMMario::CreateHero(this,eMarioLevel); CC_BREAK_IF(pMario==NULL); pMario->setPosition(TileMapPosToTileMapLayerPos(ccp(2,11))); addChild(pMario,enZOrderFront,enTagMario); //pMario->SetStatus(enMarioStatusBig); //隐藏原落坑判断层 CCTMXLayer* pTrapLayer = layerNamed("trap"); CC_BREAK_IF(pTrapLayer==NULL); pTrapLayer->setVisible(false); //初始化显示金币 CCTMXLayer* pCoinLayer = layerNamed("coin"); CC_BREAK_IF(pCoinLayer==NULL); pCoinLayer->setVisible(false); //获得地图的瓦片数量 int nMapHorizontalTileNum = pCoinLayer->boundingBox().size.width/getTileSize().width; int nMapVerticalTileNum = pCoinLayer->boundingBox().size.height/getTileSize().height; //遍历每片瓦片,寻找金币,建立并加入金币集合 for (int i = 0;i<nMapHorizontalTileNum;i++) { for (int j = 0;j<nMapVerticalTileNum;j++) { if (TileMapPosToTileType(ccp(i,j))==enTileTypeCoin) { //将瓦片地图坐标转换为瓦片地图层坐标 CCPoint CoinTileMapLayerPos = TileMapPosToTileMapLayerPos(ccp(i,j)); CMItemCoin* pCoin = CMItemCoin::CreateItemIcon(CoinTileMapLayerPos,getTileSize(),pMario,this); if (pCoin==NULL) { CCLog("Coin init Error!"); } pCoin->setPosition(CoinTileMapLayerPos); pCoin->setAnchorPoint(ccp(0,0)); m_pArrayItems->addObject(pCoin); addChild(pCoin); } } } //初始化怪物显示 CCTMXObjectGroup* pObjectLayer = objectGroupNamed("objects"); CC_BREAK_IF(pObjectLayer==NULL); CCArray *ObjectArray = pObjectLayer->getObjects(); CCDictionary *pDic = NULL; for (unsigned int i = 0; i < ObjectArray->count(); i++) { pDic = (CCDictionary *)ObjectArray->objectAtIndex(i); int PosX = ((CCString*)pDic->objectForKey("x"))->intValue(); int PosY = ((CCString*)pDic->objectForKey("y"))->intValue(); PosY -= this->getTileSize().height; CCPoint TileXY = ccp(PosX, PosY); CCString *strName = (CCString*)pDic->objectForKey("name"); CCString *strType = (CCString*)pDic->objectForKey("type"); // 进行怪物的初始化,先根据名字来判断是不是enemy,再细分enemy类型 if (strName->m_sString == "enemy") { if (strType->m_sString == "mushroom") { CMMonsterMushrooms *pMonster = CMMonsterMushrooms::CreateMonsterMushrooms(TileXY,pMario,this,this); if (pMonster==NULL) { CCLog("pMonster==NULL!"); } pMonster->setPosition(ccp(TileXY.x,TileXY.y)); pMonster->setAnchorPoint(ccp(0,0)); m_pArrayMonsters->addObject(pMonster); addChild(pMonster,enZOrderFront); } if (strType->m_sString == "tortoise") { CMMonsterTortoise *pMonster = CMMonsterTortoise::CreateMonsterTortoise(TileXY,pMario,this,this); if (pMonster==NULL) { CCLog("pMonster==NULL!"); } pMonster->setPosition(ccp(TileXY.x,TileXY.y)); pMonster->setAnchorPoint(ccp(0,0)); m_pArrayMonsters->addObject(pMonster); addChild(pMonster,enZOrderFront); } if (strType->m_sString == "flower") { CMMonsterFlower *pMonster = CMMonsterFlower::CreateMonsterFlower(TileXY,pMario,this,this); if (pMonster==NULL) { CCLog("pMonster==NULL!"); } pMonster->setPosition(ccp(TileXY.x,TileXY.y)); pMonster->setAnchorPoint(ccp(0,0)); m_pArrayMonsters->addObject(pMonster); addChild(pMonster,enZOrderFront); } } } //初始化砖块显示 CCTMXLayer* pBlockLayer = layerNamed("block"); CC_BREAK_IF(pBlockLayer==NULL); pBlockLayer->setVisible(false); //获得地图的瓦片数量 // int nMapHorizontalTileNum = pCoinLayer->boundingBox().size.width/getTileSize().width; // int nMapVerticalTileNum = pCoinLayer->boundingBox().size.height/getTileSize().height; //遍历每片瓦片,寻找瓦片,建立并加入瓦片集合 for (int i = 0;i<nMapHorizontalTileNum;i++) { for (int j = 0;j<nMapVerticalTileNum;j++) { if (TileMapPosToTileType(ccp(i,j))==enTileTypeBlock) { //解析得到当前砖块的属性 int GID = pBlockLayer->tileGIDAt(ccp(i,j)); CCDictionary *pDic = propertiesForGID(GID); CC_BREAK_IF(pDic==NULL); CCString *strBlockType = (CCString*)pDic->objectForKey("blockType"); if (strBlockType==NULL) { continue; } int nBlockType = strBlockType->intValue(); //将瓦片地图坐标转换为瓦片地图层坐标 CCPoint BlockTileMapLayerPos = TileMapPosToTileMapLayerPos(ccp(i,j)); CMItemBlock* pBlock = CMItemBlock::CreateItemBlock(BlockTileMapLayerPos,getTileSize(),pMario,this,(enumBlockType)nBlockType); if (pBlock==NULL) { CCLog("Block init Error!"); } pBlock->setPosition(BlockTileMapLayerPos); pBlock->setAnchorPoint(ccp(0,0)); m_pArrayBlocks->addObject(pBlock); addChild(pBlock); } } } return true; } while (false); CCLog("Fun CMGameMap::Init Error!"); return false; }