Cocos2d-x using tile Map

Source: Internet
Author: User
Tags map class

The complex map shown in the figure can use the tile map technology. The tile map is spliced with some small images (tiles), which can greatly reduce the memory consumption. Only three tiles are required.

Tile Map




Tiles in the map


Tile map Classification
Tile maps can be classified into three types: Cartesian maps, diagonal maps, and hexagonal maps.
1. right-angle Map
A right-angle map is built with square tiles. It looks like a bird's eye chart vertically down. The tiles that make up a tile map are not necessarily square or rectangular tiles.


2. Corner Map
A diagonal map is built with diamond tiles. A diagonal map rotates the angle of view to 45 degrees, which allows our brain to mistakenly think that we are in the 3D game world. In early electronic games, due to hardware performance limitations, many games used diagonal maps to simulate 3D games. However, running 3D games is no longer a problem. Currently, we do not recommend corner maps.


3. hexagonal Map
A hexagonal map is built with hexagonal tiles.


Right Angle Map


Diagonal Map



Hexagonal Map


Core concepts in tile Map
There are some basic concepts in tile maps that need to be understood. These concepts include layers, tiles, tile map coordinates, and global identifiers.
1. Layer
A tile map consists of a map layer. You can create, delete, hide, and display layers.
Layers can be divided into common layers and object layers. The common layer is used to draw basic map elements, which are basically fixed, such as clouds, birds, trees, cuts and lawns. Some areas can be circled in the object Layer. An area is an object used to set some attributes. We can obtain these object attributes through code. Objects in the map are associated with the genie in the scenario ,.
The middle layer of the map is ordered, so the front layer blocks the back layer. Once the layer is set to hidden, it cannot be accessed in the program.
Note that the drawing engine will draw each tile on each layer. When the design layer can meet the requirements, the fewer layers, the better. Generally, the number of layers should not exceed four layers.



The layer in the tile map (what can the todo layer Then show ?)


2. Tile
Tiles is the smallest element of the layer. Some people translate tiles into "tiles". I think "tiles" are more appropriate, in fact, the layers in the map are laid by these "tiles. The tiles required in the map are designed into a large image. These files are called "tile sets". They are free open source tile sets provided by David Gervais. We can download more tiles at http://pousse.rapiere.free.fr/tome/index.htm. The size of each tile in a tile set is the same, and there can be a fixed gap between the tiles. When used, the map design software splits the tile set into small tiles.


David Gervais tile set


3. tile map coordinates
A tile map has its own set of coordinates. The smallest unit of the map is tile, and a tile occupies a coordinate point. The coordinates of different types of maps are different. As shown in Figure 11-13, the coordinates of the right-angle tile map are located in the upper left corner. As shown in Figure 11-14, the coordinates of the diagonal tile map are located at the vertex of the diamond map.


Coordinate of right-angle tile Map



Corner tile map coordinate (remove the gray background color from Todo)


4. Global ID
Each tile in a tile map has a unique GLobal IDEntifier (GID), which is used to find a tile in the map. The global ID starts from 1. If it is 0, it indicates that the tile is empty. We can use the global ID to determine whether a tile exists under a coordinate.


Tile map API for Cocos2d-x
In order to access tile map, Cocos2d-x access tile map API, the main classes are: tmxtiledmap, tmxlayer and tmxobjectgroup.
1. tmxtiledmap
Tmxtiledmap is a tile map class, which is derived from the node class and has the node characteristics.


Tmxtiledmap class diagram
Common functions of tmxtiledmap are as follows:
? Tmxlayer * getlayer (const STD: string & layername ). Obtain the layer object by using the layer name.
? Tmxobjectgroup * getobjectgroup (const STD: string & groupname ). Obtain the object group set in the layer by using the Object layer name.
? Size & getmapsize (). Obtain the map size, which is measured in tiles.
? Size & gettilesize (). Obtain the tile size in pixels.
The sample code is as follows:
Tmxobjectgroup * Group = _ tilemap-> getobjectgroup ("objects ");
Tmxlayer * Background = _ tilemap-> getlayer ("background ");
_ Tilemap is a tile map class.


2. tmxlayer
Tmxlayer is a map layer class. Its class graph and tmxlayer are also derived from the node class and also have the node feature. At the same time, tmxlayer is also derived from the spritebatchnode class. All tmxlayer objects have batch rendering capabilities. The tile map layer is composed of a large number of repeated images, and they need to be rendered to improve performance.


Tmxlayer class diagram
Common tmxlayer functions are as follows:
? STD: string & getlayername (). Obtain the layer name.
? Size & getlayersize (). Obtain the layer size, which is measured in tiles.
? Size & getmaptilesize (). Obtain the tile size in pixels.
? Point getpositionat (const point & tilecoordinate ). Pixel coordinates are obtained through tile coordinates. the y-axis direction of tile coordinates is opposite to that of the Y-axis of pixel coordinates.
? Int gettilegidat (const point & tilecoordinate ). Obtain the GID value through the tile coordinate.


3. tmxobjectgroup
Tmxobjectgroup is a collection of object groups in the object Layer. Its Class diagram is 11-17. Note that tmxobjectgroup is different from tmxlayer. tmxobjectgroup is not derived from node and does not have the node feature.

Tmxobjectgroup class diagram
Common functions of tmxobjectgroup are as follows:
? Value getproperty (const STD: string & propertyname ). Obtain the attribute value through the attribute name.
? Valuemap GetObject (const STD: string & objectname ). Obtain object information by Object Name.
? Valuemap getproperties (). Obtain the attributes of an object.
? Valuevector getobjects (). Obtain all objects.
The alias of the valuevector type is STD: vector <value>, and vector is the c ++ container class. It can store dynamic arrays of any type, and STD is the namespace.
The alias of the valuemap type is STD: unordered_map <STD: String, value>, and unordered_map is also the container class of C ++, which is an unordered map type, map is a key-Value Pair type.

The prompt value is a generic class in a Cocos2d-x that represents data types such as unsigned char, Int, float, double, bool, STD: String, valuevector, valuemap, and valuemapintkey.


For more information, please refer to the First SWIFT book swift development guide in China.

This book discussion site: http://www.51work6.com/swift.php

Welcome to the swift technology discussion group: 362298485

Welcome to Zhijie IOS public classroom Platform


Cocos2d-x using tile Map

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.