During game development, we will encounter maps that exceed the screen size, such as real-time strategic games, so that players can scroll the game screen in the map. This type of games usually have rich background elements. If you directly use the background image switching method, you need to prepare a background image for each different scenario, and each background image is not small, which will cause a waste of resources.
Tile maps are generated to solve this problem. A large world map or background image can be represented by several kinds of terrain. Each terrain corresponds to a small image. We call these small terrain images as tiles. After splicing these tiles together, a complete map is combined. This is the principle of the tile map.
Tilemap Solution
In the Cocos2d-x, tile map implements the tilemap scheme. tilemap requires that each tile occupies a quadrilateral or hexagonal area on the map. Splice different tiles to form a complete map. We need many smaller textures to create tiles. We usually place these smaller textures in an image to improve the Drawing performance.
Tile map editor
The Cocos2d-x supports creating and saving maps in tmx format by the tile map editor tiled map editor. Tiled map editor is an open-source project that supports windows, Linux, and Mac OS X operating systems. We can download the Java and QT versions of the editor from the official website.
For details about how to use tiled to create a map, refer to the following article:
How to Use cocos2dx3.0 to create a tilemap-based game
Map direction
A tiled map supports a right-angle bird's eye view map (90 ° map), an equi-distance strabismus map (45 ° oblique map), and a hexagonal map. It does not support hexagonal maps with left and right sides or upper and lower boundaries.
Map Resources
- It is recommended that the tile map material size be a multiple of 32*32.
- Tile material groups and other images cannot be used together
- Only the tile material graph can be imported into the tmx File
- Each layer supports up to one tile clip group.
Tile layer
- The number of tiles in the tmx file is not limited.
- Each tile layer can only consist of one tile material.
- Each tile layer can be represented by the tmxlayer class-as a subclass of spritesheet.
- Each single tile is represented by Sprite-the parent node is tmxlayer
Object layer
- Tile map supports object groups
- Used to add game elements other than the background-items and obstacles
- Objects in the object group exist in the form of key-value pairs in the tmx file. Therefore, you can directly modify the object in the tmx file.
Tile map coordinate system
For the coordinate system of a 16*16 tile map file
- (0, 0): upper left corner
- (15, 15): bottom right corner
Creating a tmx node using tmx in a Cocos2d-x
12 |
Tmxtiledmap * map = tmxtiledmap: Create ("BG. tmx"); addchild (MAP, 0 ); |
Traverse subnodes
1234567891011 |
Vectorpchildrenarray = map-& gt; getchildren (); spritebatchnode * child = NULL; Ref * pobject = NULL; For (vector: iteratorit = pchildrenarray. Begin (); it! = Pchildrenarray. End (); It ++) {pobject = * it; child = (spritebatchnode *) pobject ;} |
GET/delete a tile
123 |
Tmxlayer * layer = map-& gt; getlayer ("layer0"); sprite * tile0 = Layer-& gt; gettileat (point (1, 15); layer-& gt; removetileat (point (1, 15 )); |
Traverse objects in the object Layer
12345678 |
Tmxobjectgroup * objectgroup = map-& gt; getobjectgroup ("center"); valuevector object = objectgroup-& gt; getobjects (); For (valuevector: iteratorit = object. begin (); it! = Object. end (); It ++) {valueobj = * it; valuemap map = obj. asvaluemap (); log ("x = % d y = % d", map. at ("X "). asint (), map. at ("Y "). asint ());} |