Cocos2d-x Study Notes (23) -- map use 3 -- CCTMXLayer

Source: Internet
Author: User

Cocos2d-x Study Notes (23) -- map use 3 -- CCTMXLayer

Note: From cocos2d-x Study Notes (21) I used the cocos2d-x version 2.0.2, while the version I used earlier was 1.0. Version 2.0.2 fixes many bugs, including the bugs that occur when CCTMXLayer uses the addChild function ). There are some differences between the two versions, which may cause some inconvenience for everyone to read. I would like to apologize to you here. I hope you can pay more attention to the differences, I will modify the previous article later, how to Use version 2.0.2 on the annotation ...........................

In the cocos2d-x study note (15)-map use 1 (TMX) section, I mainly talked about the basic use of tmx landlords: including the use of the tiled tool, loading the tmx map, and obtaining the objectGroup in the map. Later, I actually tried some small games and found that this knowledge is far from enough. It is necessary to learn more about the use of maps, as a result, I plan to write several more articles on map usage to introduce more tmx map usage. I hope this will be helpful for you to develop games later. Now, start cutting into the topic, but before you start, we suggest you take a look at the cocos2d-x Study Notes (22) -- map use 2 (TMX) in the section -- Z-Order, AnchorPoin, and anti-aliasing, we will first understand some concepts.


Step 1: first create a cocos2d-win32 project and name it MapTest2.

Step 2: Add a class in HelloWorldScene. h:
[Html]
Class MapTest2: public CCLayer
{
Protected:
CCSprite * m_player;
Public:
MapTest2 ();
~ MapTest2 ();
Void repositionSprite (float dt );
};


Add the following code to HelloWorldScene. cpp:

[Html]
/*************************************** *********************************/
/* MapTest2 */
/*************************************** *********************************/
MapTest2: MapTest2 ()
{
CCTMXTiledMap * map = CCTMXTiledMap: create ("map. tmx ");
AddChild (map, 0, kTagMap );
CCSize s = map-> getContentSize ();
CCSize winSize = CCDirector: sharedDirector ()-> getWinSize ();
Map-> setAnchorPoint (ccp (0.5, 0.5 ));
// If AnchorPoint is not set, the default AnchorPoint of map is (0, 0), that is, the lower left corner is not the map center we want.
Map-> setPosition (ccp (winSize. width/2, winSize. height/2 ));
 
M_player = CCSprite: create ("player.png ");

Map-> addChild (m_player, map-> getChildren ()-> count ());
M_player-> retain ();
Map-> reorderChild (m_player, 0 );
Int mapWidth = map-> getMapSize (). width * map-> getTileSize (). width;
Int mapHeight = map-> getMapSize (). height * map-> getTileSize (). height;
M_player-> setPosition (ccp (mapWidth/2, mapHeight/2 ));
// Because m_player is added to map, setPosition is relative to the map parent coordinate system,
// So we set the ccp (mapWidth/2, mapHeight/2) instead of the center of the screen coordinate system.
Map-> reorderChild (m_player, 0 );
// Set the Zorder value of the genie object so that the genie can travel through the grass
 
// CCArray * array = map-> getChildren ();
// CCObject * object = NULL;
// CCSpriteBatchNode * child = NULL;
// CCARRAY_FOREACH (array, object)
//{
// Child = (CCSpriteBatchNode *) object;
// If (! Child)
// Return;
// Child-> getTexture ()-> setAntiAliasTexParameters ();
//} // Set anti-aliasing. If you need to zoom in or out a map, you can use
 
CCActionInterval * move = CCMoveBy: create (10, ccp (300,250 ));
CCActionInterval * back = move-> reverse ();
CCFiniteTimeAction * seq = CCSequence: create (move, back, NULL );
M_player-> runAction (CCRepeatForever: create (CCActionInterval *) seq ));
}
 
MapTest2 ::~ MapTest2 ()
{
M_player-> release ();
}
At this time, when you run the program, you can see the effect of the wizard moving through the map.

The blank area in the map is my own background transparency processing. When creating a map, I created two map layers, one being the ground layer, used to display the earth surface, and the other being the grass layer for setting up grass.

Before proceeding to the following code, I will introduce a function because it will be used below:
[Html]
// Returns the delta position between the current location and the previous location in OpenGL coordinates
CCPoint CCTouch: getDelta () const
{
Return ccpSub (getLocation (), getpreviuslocation ());
}

In this case, we cannot drag the map. We need to add the following touch screen response function:
[Html]
Void MapTest2: registerWithTouchDispatcher ()
{
CCDirector * pDirector = CCDirector: shareddire ();
PDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );
}
Bool MapTest2: ccTouchBegan (CCTouch * touch, CCEvent * event)
{
Return true;
}
 
Void MapTest2: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent)
{
CCPoint diff = pTouch-> getDelta ();
CCTMXTiledMap * map = (CCTMXTiledMap *) getChildByTag (kTagMap );
CCPoint currentPos = map-> getPosition ();
Map-> setPosition (ccpAdd (currentPos, diff ));
}
Compile and run the program again. This time, you can drag the map to see the full picture of the map.


At the end of the constructor, add the schedule (schedule_selector (MapTest2: update), 1.0f) function, and add the void update (float dt) member function to the class.
[Html]
Void MapTest2: update (float dt)
{
CCTMXTiledMap * map = (CCTMXTiledMap *) getChildByTag (kTagMap );
CCTMXLayer * layer = map-> layerNamed ("grass"); // obtain the layer
 
CCPoint coordinate = ccp (10, 22); // the coordinates in the tmx map. The size of a tile is the basic unit of the map.
// Int flags;
// Unsigned int GID = layer-> tileGIDAt (coordinate, (ccTMXTileFlags *) & flags); // The third parameter is the ccTMXTileFlags pointer type.
// If (flags = kCCTMXTileVerticalFlag)
// Flags & = ~ KCCTMXTileVerticalFlag;
// Else
// Flags | = kCCTMXTileVerticalFlag;
// Layer-> setTileGID (GID, coordinate, (ccTMXTileFlags) flags );
 
CCSprite * sprite = layer-> tileAt (coordinate );
Sprite-> runAction (CCRotateBy: create (1, 45 ));
}


Let's take a look at the definition of ccTMXTileFlags:

Typedef enum ccTMXTileFlags _{
KCCTMXTileHorizontalFlag = 0x80000000,
KCCTMXTileVerticalFlag = 0x40000000,
KCCTMXTileDiagonalFlag = 0x20000000,
KCCFlipedAll = (kCCTMXTileHorizontalFlag | kCCTMXTileVerticalFlag | kCCTMXTileDiagonalFlag ),
KCCFlippedMask = ~ (KCCFlipedAll)
} CcTMXTileFlags;
Unsigned int GID = layer-> tileGIDAt (coordinate, (ccTMXTileFlags *) & flags); stores the value of the ccTMXTileFlags constant of the tile under the obtained coordinate in flags.
As
If (flags = kCCTMXTileVerticalFlag)
Flags & = ~ KCCTMXTileVerticalFlag;
Else
Flags | = kCCTMXTileVerticalFlag;
I have not understood the principles of this part of the code. If the netizen knows this, I hope to give some advice and share it with you.
As a compensation, we can achieve similar results through the following code:
CCSprite * sprite = layer-> tileAt (coordinate );
Sprite-> runAction (CCRotateBy: create (1, 45 ));
In a tmx map, every tile is actually a CCSprite. We can use CCTMXLayer to obtain its CCSprite Based on the coordinates of the tile and adjust the tile.
Run the program again and drag the map to find a place where the fence is continuously rotating.

 

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.