Cocos2d-x Tiled地圖編輯器(一)基本使用

來源:互聯網
上載者:User

Tiled地圖編輯器支援普通視角地圖和45度角地圖, 它產生的地圖資料檔案cocos2d-x完美的支援,Tiled地圖編輯器是一個以普通使用為目標地圖編輯器,它使用簡單並且可以輕鬆地在不同的遊戲引擎中使用,其特性有:

1、使用基於XML編碼形的地圖資料檔案使用可以在不同遊戲引擎中通用

2、支援普通、45度兩種視角

3、對象的置放位置可以精確到像素

4、支援圖素、層次和對象等通用概念

5、自動重新載入圖素集

6、可以重設圖素的大不和位移

7、支援圖章刷和填充等高效工具

8、支援以通用的格式輸入輸出來開啟和隱藏檔

 

開始Tiled地圖編輯器編輯地圖

一、首先準備地圖素材檔案放至工程Resources下,下載安裝tiled-0.9.1-win32-setup.exe

地址:http://www.mapeditor.org/

 

二、啟動Tiled,選擇“檔案-》新地圖”,來建立地圖工程,彈出如下對話方塊設定地圖的大小和圖塊大小和地圖視角方向



三、選擇“地圖-》新圖塊”匯入圖素檔案,彈出如下對話方塊設定圖塊的大小、邊距、位移量及圖塊名稱、源路徑



四、圖塊建立成功,右側顯示圖層名稱及圖塊視窗中的圖塊,將圖層名修改一下,點擊工具列章刷,點擊一圖塊開始畫地圖



五、在圖層視窗添加對象層並重新命名,並在對象層添加對象,選中建立的對象層,點擊工具列上“插入矩形”在地圖上畫一個矩形,大小無關緊要,我們主要用來在地圖

擷取該x,y座標,以在此放至精靈,右鍵剛剛添加矩形,選擇對象屬性,給它命個名稱,然後點擊確定。





六、點擊儲存地圖命名*.tmx至Resources


七、編寫程式碼:

CCTMXTiledMap瓦片地圖集類是cocos2d-x中支援Tiled地圖編碼資料檔案形式的類,用於解析地圖集的資料檔案。開始使用地圖,加入代碼:

CCTMXTiledMap *pTMXTiledMap = CCTMXTiledMap::create("map.tmx");pTMXTiledMap->setScale(0.8f);pTMXTiledMap->setAnchorPoint( ccp(0.5f, 0.5f) );pTMXTiledMap->setPosition(ccp(visibleSize.width/2 , visibleSize.height/2-300));this->addChild(pTMXTiledMap);


運行結果:

 

 

八、Cocos2d-x 操作Tiled地圖常用方法

顯示Tiled地圖

CCTMXTiledMap *map = CCTMXTiledMap::create("map3.tmx");map->setAnchorPoint( ccp(0.5f, 0.5f) );map->setPosition(ccp(visibleSize.width/2, visibleSize.height/2));this->addChild(map);


擷取地映像素大小, width等於地圖寬度塊數*每塊寬度,height 等於地圖高度塊數*每塊高度

CCSize CC_UNUSED s = map->getContentSize();CCLOG("ContentSize: %f, %f", s.width,s.height);


擷取地圖層

CCTMXLayer *layer = map->layerNamed("layer1");//參數:地圖層名稱CCSize m = layer->getLayerSize();//地圖大小CCLOG("LayerSize: %f, %f", m.width,m.height);


擷取對象層

CCTMXObjectGroup *object = map->objectGroupNamed("object1");//參數:對象層名稱


擷取對象

CCDictionary *sprite_object = object->objectNamed("sprite1");//參數:對象名稱


擷取對象座標

float x = ((CCString*)sprite_object->objectForKey("x"))->floatValue();float y = ((CCString*)sprite_object->objectForKey("y"))->floatValue();


在座標處加入精靈

CCSprite *sprite = CCSprite::create("sprite.png");sprite->setScale(0.5f);sprite->setAnchorPoint(ccp(0.0f, 0.0f));sprite->setPosition(ccp(x, y));this->addChild(sprite);


當有多個地圖層時,遍曆地圖層

CCArray* pChildrenArray = map->getChildren();CCSpriteBatchNode* child = NULL;CCObject* pObject = NULL;CCARRAY_FOREACH(pChildrenArray, pObject){child = (CCSpriteBatchNode*)pObject;if(!child)break;child->getTexture()->setAntiAliasTexParameters();}


當有對象層有多個對象時,遍曆所有對象

CCArray *obs = object->getObjects();CCDictionary *dict=NULL;CCObject *ob = NULL;CCARRAY_FOREACH(obs, ob){dict = (CCDictionary *)ob;if (!dict)break;int y = ((CCString*)dict->objectForKey("y"))->floatValue();int x = ((CCString*)dict->objectForKey("x"))->floatValue();int w = ((CCString*)dict->objectForKey("width"))->floatValue();int h = ((CCString*)dict->objectForKey("height"))->floatValue();CCLOG("sprite x: %d, y: %d, w: %d, h: %d", x, y, w, h);}



最後結果圖:


普通視角擷取地圖層四角圖素

CCTMXLayer *layer = map->layerNamed("layer1") ;CCSize s = layer->getLayerSize();CCSprite* sprite;sprite = layer->tileAt(ccp(0,0));sprite->setScale(2);sprite = layer->tileAt(ccp(s.width-1,0));sprite->setScale(2);sprite = layer->tileAt(ccp(0,s.height-1));sprite->setScale(2);sprite = layer->tileAt(ccp(s.width-1,s.height-1));sprite->setScale(2);


將精靈作為了節點加入地圖中
m_tamara = CCSprite::create("nan.png");map->addChild(m_tamara, map->getChildren()->count() );//將精靈作為子節點加入


修改精靈與地圖遮擋物關係

//修改z軸的值並排序CCPoint p = m_tamara->getPosition();p = CC_POINT_POINTS_TO_PIXELS(p);CCNode *map = getChildByTag(kTagTileMap);int newZ = 4 - (p.y / 48);newZ = max(newZ,0);map->reorderChild(m_tamara, newZ); 



 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.