Summary of cocos2d-x using tile map

Source: Internet
Author: User

Summary of cocos2d-x using tile map

Summary of cocos2d-x using tile map

Using tile map to play games can greatly improve development efficiency. Recently, new games have also been developed using tile map.
Based on your experience in using tile map and some references, the usage includes:

1. The most basic thing is to make a game map. After importing materials, use a brush to fl the desired topographic map. The code for creating a map and a background layer is as follows:
    auto tileMap = TMXTiledMap::create(try1.tmx);    auto backgoround = tileMap->getLayer(background);    addChild(tileMap, 1);
2. Game characters and interfaces can be directly located in the map, and loaded into the Sprite or interface, such as the scoreboard and buttons; of course, it is more convenient to use cocos studio and combine it with tile map. Step: 1. Choose layer> Add object Layer: 2. then we can draw a matrix when selecting the object Layer. This matrix is not displayed on the game screen, but it can help us locate 3. then, use the code in the game to locate the matrix, and place the desired object as follows:
cocos2d::TMXObjectGroup *playerObjGroup = tileMap->getObjectGroup(player_object_group);    auto playerPos = playerObjGroup->getObject(player_position_1);    float x = playerPos[x].asFloat();    float y = playerPos[y].asFloat();    player = Sprite::create(029.png);    player->setPosition(x, y);    addChild(player, 1);
3. Set collision and game events as follows:
    int tileGid = block->getTileGIDAt(tileCoord);    if (tileGid) {        auto properties = tileMap->getPropertiesForGID(tileGid).asValueMap();        if (!properties.empty()) {            auto collision = properties[blockage].asString();                if (true == collision) {                    return;                }///
  
4. game level design and configuration: using tile map, you can locate the places where monsters or obstacles are placed. You can also customize the attributes that need to be attached as needed to meet the level editing requirements, for example, you can use different custom attributes to define the types of monsters and obstacles and the appearance of different types of monsters. For example, the following code simply leads the enemy to appear in different positions on the map, the "Enemy" attribute is also customized. Here it defines the type of the Enemy. You can also define the image resources, attack power, and when the Enemy will use as needed ...... The practice is to satisfy many needs as imagined.
For (auto & epos: enemyObjGroup-> getObjects () {ValueMap & dict = epos. asValueMap (); if (dict [Enemy]. asInt () = 1) {x = dict [x]. asInt (); y = dict [y]. asInt (); addEnemys (Point (x, y); // <custom function for adding enemies }}

Of course, tile map is very powerful. I have not summarized many other usage methods.
This article is for reference only and is not detailed.

Finally sp cocos2d-x using tile map a super pitfall:
If a new layer is created in tile map, but no graphic block is painted in the new layer, the entire game crashes when the cocos2d-x reads such a tmx file !!! I tried it several times before trying out the real cause! It is very likely that some people may not be able to extricate themselves, and then they have to re-create a new map, and then they are not careful ......

 

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.