Cocos2d-x using tile Map

Source: Internet
Author: User
Tags map class

Cocos2d-x using tile Map

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. Vector is the C ++ container class, which can store dynamic arrays of any type, and std is the namespace.
The alias of the ValueMap type is std: unordered_map. , Unordered_map is also the C ++ container class, which is an unordered map type, and map is the "key-value" 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.

 


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.