The cocos2dx engine uses the cctmxtilemap class to represent a tile map (also known as a brick map). It mainly contains the following four types of elements:
- Brick element: Specifies the texture image in the tmx file (which is not necessarily correct );
- Cctmxlayer: This is the background map we see. The engine uses the genie ccsprite to organize the brick elements, including the brick size and many other information;
- Cctmxobjectgroup: It may be used to organize buildings, etc.
- Attributes: mainly attributes such as map size.
First, you need to use the tield tile map editor to create a tmx file (use the tutorial) named "tilemaptest. tmx ". Then, place the file and the corresponding PNG Image under the resources \ tilemap directory of the project.
Define the map Loading Function in the header file.
void loadTileMap();
Implementation in the CPP file:
void HelloWorld::loadTileMap(){ CCTMXTiledMap* map = CCTMXTiledMap::create("TileMap/TileMapTest.tmx"); addChild(map,-1,100);}
Note: by default, the files used by the cctmxtiledmap: Create method start searching for files under the Resources Directory of the project. Finally, call the function in the init method. The running effect is as follows (only a simple map is loaded, the map itself is relatively large, and many of them are not displayed ):