Cocos2d-x Tiled map editor (I) basic use

Source: Internet
Author: User

The Tiled map editor supports both common view maps and 45-degree angle maps, and the cocos2d-x of the map data file is perfectly supported. The Tiled map editor is a target map editor for common use, it is easy to use and can be easily used in different game engines. Its features include:

1. XML-encoded map data files can be used in different game engines

2. Supports common and 45-degree perspectives

3. Object placement can be precise to pixels

4. General concepts such as graphic elements, layers, and objects are supported.

5. automatically reload the image set

6. You can reset the big discord and offset of the Graphic Element.

7. Support for efficient tools such as stamp brush and filling

8. allows you to open and store files in a common format.

 

Start Tiled map editor to edit Map

I,First download the map Material file to the project resources, download and install tiled-0.9.1-win32-setup.exe

Address: http://www.mapeditor.org/

 

2. Start Tiled and select "file-" New Map "to create a map project. The following dialog box is displayed to set the map size and map block size and map angle direction.



3. Select "Map-new map block" to import the map element file. The following dialog box is displayed to set the size, margin, offset, block name, and Source Path of the map.



4. The image block is successfully created. The name of the layer and the block in the graph block window are displayed on the right. Modify the layer name, click the tab in the toolbar, and click a block to draw the map.



5. Add and rename the object Layer in the Layers window, add objects to the object Layer, select the newly created Object layer, and click "insert rectangle" on the toolbar to draw a rectangle on the map, the size does not matter. We mainly use

Get the x and y coordinates, place them here to the genie, right-click the rectangle you just added, select the object property, give it a name, and then click OK.





6. Click Save map name *. tmx to Resources


7. Compile the program code:

CCTMXTiledMap tile gallery class is a type of cocos2d-x that supports the form of Tiled map encoding data files, used to parse the Atlas data files. Start using the map and add the Code:

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);


Running result:

 

 

8. Common Methods of Tiled map operation in Cocos2d-x

Display Tiled Map

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);


Obtain the map pixel size. The width is equal to the number of map width blocks * the width of each block, and the height is equal to the number of map height blocks * the height of each block

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


Get map layer

CCTMXLayer * layer = map-> layerNamed ("layer1"); // parameter: map layer name CCSize m = layer-> getLayerSize (); // map size CCLOG ("LayerSize: % f, % f ", m. width, m. height );


Get object Layer

CCTMXObjectGroup * object = map-> objectGroupNamed ("object1"); // parameter: object layer name


Get object

CCDictionary * sprite_object = object-> objectNamed ("sprite1"); // parameter: object Name


Get object coordinates

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


Add genie to coordinates

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);


When there are multiple map layers, traverse the map layer

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


When there are multiple objects in the object Layer, traverse all objects

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);}



Final result diagram:


Obtain the four-corner map element of the map layer from the normal perspective

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);


Add the genie to the map as a node
M_tamara = CCSprite: create ("nan.png"); map-> addChild (m_tamara, map-> getChildren ()-> count (); // Add the genie as a subnode


Modify the relationship between the genie and the map shelter

// Modify the value of the Z axis and sort 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 );



 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.