Using tile map in Cocos2d-X II

Source: Internet
Author: User

Using tile map in Cocos2d-X II

In the previous blog introduced the use of tile map in the Cocos2d-X, In this blog will go on the previous blog introduced tile map in the Cocos2d-X more advanced application,

Use the map editor to open the previous tile map.


Effect of adding an Object layer to the map editor


After saving the file, observe the tmx file of the map and find that there is an additional section in the file.

   
    
    
   
 


Code explanation:
Objectgroup name = "OBJECT1" indicates the name of the object Layer

X = "113" x coordinate of an object in the object Layer

Y = "589" y coordinate of an object in the object Layer

Width = "12" width of the Object layer (12 indicates that the width of the Object layer is 12 graph blocks)

Height = "12" height of the Object layer (12 indicates that the height of the Object layer is 12 graph blocks)

Width = "190" the width of an object in the object Layer

Height = "95" height of an object in the object Layer


Program Example 1: print the information of each object in the object Layer

Program code

// Create map CCTMXTiledMap * map = CCTMXTiledMap: create ("orthogonal-test1.tmx"); addChild (map ); // obtain the object CCTMXObjectGroup * objG = map-> objectGroupNamed ("OBJECT1") through the Object layer name; // obtain the object array CCArray * objs = objG-> getObjects (); // obtain the number of objects int count = objs-> count (); for (int I = 0; I
 
  
ObjectAtIndex (I); // converts an object to a dictionary CCDictionary * dict = (CCDictionary *) obj; // obtain the x coordinate of the object const CCString * x = dict-> valueForKey ("x "); // obtain the x coordinate of the object const CCString * y = dict-> valueForKey ("y "); // obtain the object width const CCString * width = dict-> valueForKey ("width "); // obtain the height of the object const CCString * height = dict-> valueForKey ("height"); // print the object information CCLog ("x = % d, y = % d, width = % d, height = % d ", x-> intValue (), y-> intValue (), width-> intValue (), height-> intValue ());}
 

Execution result:


By comparing the object data in the tmx file and the output object data, we can find that the y coordinate of the object is different from the y coordinate of the output object, because the tile map uses the coordinate origin position in the upper left corner, while the origin of the coordinates in the Cocos2d-X is the lower left corner, the output is the coordinates of the object in the Cocos2d-X


Program instance 2: Add a genie on the object Layer

First, use the map editor to create a tile map.


Add an object Layer on the layer, add an object on the object layer, and save


The added tmx file is

  
    
    
   
    
   EJyTY2Bg0ANiOTRsgMTGJo + uTh6LOgUktj4Oc7iIwNjcpI4mhw3D5HHZS0ivHpp + authorization = 
   
    
    
  


Then add an image to the Resource folder to create the genie.


Program code:

// Create map CCTMXTiledMap * map = CCTMXTiledMap: create ("map. tmx "); addChild (map); // get the object Layer CCTMXObjectGroup * objG = map-> objectGroupNamed (" OBJECT1 ") through the Object layer name "); // obtain the object CCDictionary * dir = objG-> objectNamed ("Hero") in the object Layer through the object name "); // obtain the Object Location float x = dir-> valueForKey ("x")-> floatValue (); float y = dir-> valueForKey ("y ") -> floatValue (); // create a sprite on the object: CCSprite * sprite = CCSprite: create ("green_edit.png"); addChild (sprite ); // set sprite-> setPosition (ccp (x, y) for the sprite. // set sprite-> setAnchorPoint (CCPointZero) in the lower left corner of the sprite );

Execution result:


Zookeeper

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.