您好,登錄后才能下訂單哦!
cocos2d-x地圖的應(yīng)用最典型的例子就是塔防類游戲,下面我們就來簡單學(xué)習(xí)一下地圖的應(yīng)用吧!
我們應(yīng)用的地圖編輯軟件是Tiled,一款免費(fèi)的軟件,首先是文件->新文件,然后地圖->新圖塊導(dǎo)入圖片資源,然后就可以在地圖上繪制。
Tiled有四種方式可以編輯瓷磚地圖,菜單位于軟件界面的右上角。
Stamp Brush(快捷鍵 B),可以把當(dāng)前從瓷磚集中選擇的瓷磚畫在地圖上;
Bucket Fill(快捷鍵 F),可以填充空白區(qū)域或者由相同瓷磚連接起來的區(qū) 域;
Eraser(快捷鍵 E),用于擦掉瓷磚;
Rectangular Select(快捷鍵 R), 你可以選擇一個區(qū)域,然后用Ctrl+c或者菜單項(xiàng)Edit > Copy來復(fù)制選中的區(qū)域, 接著Ctrl+v或者菜單項(xiàng)Edit > Paste粘貼到當(dāng)前鼠標(biāo)在地圖上所處的位置 – 在實(shí)際使用中你會看到,其實(shí)在粘貼時,軟件是自動卻換到了Stamp Brush模式 進(jìn)行粘貼的。還有個小技巧,如果想清除當(dāng)前的瓷磚選擇,只要鼠標(biāo)右鍵點(diǎn)擊一下就可以了。
一個圖層上面不得使用兩個圖片上的資源,不然后面調(diào)用的代碼編譯會報(bào)錯;
其次就是將圖片資源和生成的tmx文件要保持在同級路徑下,不然后面代碼也會報(bào)錯,當(dāng)然也可以修改tmx下的圖片資源路徑來解決問題。
1.點(diǎn)擊屏幕獲取坐標(biāo)點(diǎn)
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCPoint point = this->tilePosFromLocation(pTouch->getLocation(), (CCTMXTiledMap *)this->getChildByTag(1)); CCLog("%f,%f",point.x,point.y); return true; } CCPoint HelloWorld:: tilePosFromLocation(CCPoint location,CCTMXTiledMap *tileMap) { //pos是地圖上的坐標(biāo),當(dāng)前屏幕的坐標(biāo)+地圖的偏移量 CCPoint pos = ccpSub(location, tileMap->getPosition()); pos.x = (int)(pos.x / tileMap->getTileSize().width); //(地圖總塊數(shù)*每塊的像素 - 現(xiàn)在的y坐標(biāo))/ 每塊的像素 //getMapSize().height是地圖高度的瓷磚數(shù) pos.y = (int)(((tileMap->getMapSize().height * tileMap->getTileSize().height - pos.y)) / tileMap->getTileSize().height); CCLog("%f,%f",pos.x,pos.y); return pos; }
//獲取瓷磚的屬性 CCPoint playerindex = ccp(a,b); //根據(jù)坐標(biāo)點(diǎn)獲取GID int tiledid = layer->tileGIDAt(playerindex); if (tiledid > 0) { //根據(jù)GID獲取指定瓷磚塊的屬性 CCDictionary *tiledic = (CCDictionary *)tileMap->propertiesForGID(tiledid); //判斷瓷磚塊是否有屬性,如果有就用它的屬性,如果沒有 if (tiledic) { CCString * value = (CCString *)tiledic->objectForKey("isTree"); int va = value->intValue(); if (va == 1) { CCLog("這是一棵樹"); } } else { CCLog("該瓷磚快沒有屬性"); } }
Cocos2d: a:12,b:2
Cocos2d: 這是一棵樹
/**************獲取對象圖層********************************/ CCTMXObjectGroup * objectLayer = tileMap->objectGroupNamed("objLayer"); //判斷觸摸點(diǎn)是否在對象圖層上 bool isTouchInRectangle = false; //獲取對象圖層的對象數(shù)目 int numObjects = objectLayer->getObjects()->count(); for (int i=0; i<numObjects; i++) { //獲取到這個對象的屬性 CCDictionary * properties = (CCDictionary *)objectLayer->getObjects()->objectAtIndex(i); CCRect rect = getRectFromObjectPropertier(properties, tileMap); //看看觸摸點(diǎn)是否在對象圖層上 if (rect.containsPoint(location)) { isTouchInRectangle = true; break; } } if (isTouchInRectangle) { CCLog("碰到圖層了"); }
CCRect HelloWorld::getRectFromObjectPropertier(cocos2d::CCDictionary *properties, cocos2d::CCTMXTiledMap *tileMap) { //這里的valueForKey的四個參數(shù)是圖層中默認(rèn)有的屬性 float x,y,width,height; x = properties->valueForKey("x")->floatValue()+tileMap->getPosition().x; y = properties->valueForKey("y")->floatValue()+tileMap->getPosition().y; width = properties->valueForKey("width")->floatValue(); height = properties->valueForKey("height")->floatValue(); CCRect pos = CCRectMake(x, y, width, height); return pos; }
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent) { CCPoint point = this->tilePosFromLocation(pTouch->getLocation(), (CCTMXTiledMap *)this->getChildByTag(1)); //CCLog("%f,%f",point.x,point.y); //點(diǎn)擊讓地圖移動 this->centerTileMapOnTileCoord(point,(CCTMXTiledMap *)this->getChildByTag(1)); return true; }
//讓圖層移動的方法 void HelloWorld::centerTileMapOnTileCoord(cocos2d::CCPoint tilePos, cocos2d::CCTMXTiledMap *tileMap) { CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); CCPoint screenCenter = CCPointMake(screenSize.width / 2, screenSize.height / 2); tilePos = CCPointMake(tilePos.x, (tileMap->getMapSize().height - 1) - tilePos.y); CCPoint scrollPosition = ccp(-(tilePos.x*tileMap->getTileSize().width),-(tilePos.y*tileMap->getTileSize().height)); scrollPosition.x+=screenCenter.x-tileMap->getTileSize().width/2; scrollPosition.y+=screenCenter.y-tileMap->getTileSize().height/2; scrollPosition.x=(MIN(scrollPosition.x, 0)); scrollPosition.x=(MAX(scrollPosition.x, -screenSize.width)); scrollPosition.y= (MIN(scrollPosition.y, 0)); scrollPosition.y= (MAX(scrollPosition.y, -screenSize.height)); //移動 CCAction * move = CCMoveTo::create(0.2f, scrollPosition); tileMap->stopAllActions(); tileMap->runAction(move); }
//如果是DEBUG版本就會出現(xiàn),如果是RELEASE版本就不會出現(xiàn)這段代碼 #ifdef DEBUG //畫圖 //draw函數(shù)是系統(tǒng)自動不斷調(diào)用的 void HelloWorld::draw() { //獲取地圖 CCNode * node = this->getChildByTag(1); CCTMXTiledMap * tileMap = (CCTMXTiledMap *)node; //獲取對象圖層 CCTMXObjectGroup * objectLayer = tileMap->objectGroupNamed("objLayer"); //設(shè)置畫筆顏色 ccDrawColor4B(255, 0, 0, 255); //一次遍歷對象圖層中每一塊瓷磚 for (int i = 0; i<objectLayer->getObjects()->count(); i++) { CCDictionary * properties = (CCDictionary *)objectLayer->getObjects()->objectAtIndex(i); CCRect rect = this->getRectFromObjectPropertier(properties, tileMap); this->drawRect(rect); } CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); CCPoint center = CCPointMake(screenSize.width/2, screenSize.height/2); ccDrawCircle(center, 10, 240, 8, true); //線的寬度 glLineWidth(2.0f); } void HelloWorld::drawRect(CCRect rect) { CCPoint pos1,pos2,pos3,pos4; pos1 = CCPointMake(rect.origin.x, rect.origin.y); pos2 = CCPointMake(rect.origin.x, rect.origin.y + rect.size.height); pos4 = CCPointMake(rect.origin.x + rect.size.width, rect.origin.y); pos3 = CCPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); ccDrawLine(pos1, pos2); ccDrawLine(pos2, pos3); ccDrawLine(pos3, pos4); ccDrawLine(pos4, pos1); } #endif
注意:地圖的z軸屬性要設(shè)置低一點(diǎn),不然紅線顯示不出來被地圖遮蓋了。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。