[Cpp]
Cocos2d-x Study Notes (15)-map use (TMX)
Before talking about how to use maps, I would like to introduce you to the open source map editing software (. tmx) under a cocos2d-x ). Here I use the QT version.
The interface after opening the software is as follows:
Here I use the image Resources in the example of a game engine (ortho-test2.png under .. C: \ cocos2d-x \ tests \ Resources \ tilemaps ).
The first step is to create a new file. Here I set the size and block size of the new map as follows:
Under the map option, select a new layer and set it as follows:
The final result is as follows:
After editing the map, name it map and save it, save map. tmx and the ortho-test2 to the Resources folder under the new project, followed by the Code Section:
Step 1: Create the cocos2d-win32 project and name it tileMap;
Step 2: Add the following classes in HelloWorldScene. h:
[Cpp]
Class MapLayer: public CCLayer
{
Protected:
CCSprite * m_player;
Public:
MapLayer ();
~ MapLayer ();
};
Step 3: Add the following functions to HelloWorldScene. cpp:
Add the following code to the MapLayer () constructor:
[Cpp]
SetIsTouchEnabled (true );
CCTMXTiledMap * map = CCTMXTiledMap: tiledMapWithTMXFile ("map. tmx ");
AddChild (map, 1, tagTileMap );
Map-> setPosition (ccp (0, 0 ));
Compile and run the code to view a part of the map;
Next, in order to see other parts of the map, you need to add the screen response function:
Therefore, add the following functions to the MapLayer class:
[Cpp]
Class MapLayer: public CCLayer
{
Protected:
CCSprite * m_player;
Public:
MapLayer ();
~ MapLayer ();
Void registerWithTouchDispatcher ();
Bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
Void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent );
Void ccTouchCancelled (CCTouch * pTouch, CCEvent * pEvent );
Void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent );
};
Then add the Response Function in HelloWorldScene. cpp:
[Cpp]
MapLayer ::~ MapLayer ()
{
M_player-> release ();
}
Void MapLayer: registerWithTouchDispatcher ()
{
CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this, 0, true );
}
Bool MapLayer: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent)
{
Return true;
}
Void MapLayer: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent)
{
CCPoint touchLocation = pTouch-> locationInView (pTouch-> view ());
CCPoint prevLocation = pTouch-> previuslocationinview (pTouch-> view ());
TouchLocation = CCDirector: sharedDirector ()-> convertToGL (touchLocation );
PrevLocation = CCDirector: sharedDirector ()-> convertToGL (prevLocation );
CCPoint diff = ccpSub (touchLocation, prevLocation );
// CcpAdd is used to subtract the x y coordinates of currentPos and diff.
CCNode * node = getChildByTag (tagTileMap );
CCPoint currentPos = node-> getPosition ();
Node-> setPosition (ccpAdd (currentPos, diff ));
// CcpAdd adds the x y coordinates of currentPos and diff.
// The main meaning here is the prevLocation (x1, y1) coordinate of the mouse start clicking)
// Move the cursor to the coordinate touchLocation (x2, y2) of a certain point ),
// The difference between the two coordinates is the distance and direction of the map to be moved.
// Node-> setPosition (ccpAdd (currentPos, diff); is to move the x2-x1 on the original coordinates of the map, y2-y1
}
Void MapLayer: ccTouchCancelled (CCTouch * touch, CCEvent * event)
{
}
Void MapLayer: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent)
{
[Cpp]
[Cpp]
[Cpp]
Now run the program again, and you can drag the map to see the rest of the map;
Next, to enrich the content on the map, I add a character on the map and can move the character at will. Here I will use the object Layer knowledge. Return to the map editor, click the layer button, and select Add Object layer. Name it "object" and click "insert object. Select a position for adding a person on the map. This is a box displayed on the map. Click the box, right-click the attribute, and enter player in the name column;
Next, add the following code to the MapLayer constructor:
[Cpp]
MapLayer: MapLayer ()
{
SetIsTouchEnabled (true );
CCTMXTiledMap * map = CCTMXTiledMap: tiledMapWithTMXFile ("map. tmx ");
AddChild (map, 1, tagTileMap );
Map-> setPosition (ccp (0, 0 ));
CCTMXObjectGroup * objGroup = map-> objectGroupNamed ("object ");
CCStringToStringDictionary * spawnPoint = objGroup-> objectNamed ("player ");
Float x = spawnPoint-> objectForKey ("x")-> toFloat ();
Float y = spawnPoint-> objectForKey ("y")-> toFloat ();
CCTexture2D * playerTexture = CCTextureCache: sharedTextureCache ()-> addImage ("Player.png ");
M_player = CCSprite: spriteWithTexture (playerTexture );
M_player-> retain ();
M_player-> setPosition (ccp (x, y ));
Map-> addChild (m_player );
}
Run the code again and drag the map at will. A character is displayed on the map;
Now, the basic operations on the map have been completed. Finally, let's modify the ccTouchEnded function to let the person move Based on the mouse clicking position;
[Cpp]
Void MapLayer: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent)
{
CCPoint location = pTouch-> locationInView (pTouch-> view ());
Location = CCDirector: sharedDirector ()-> convertToGL (location );
CCActionInterval * action = CCMoveTo: actionWithDuration (3, location );
M_player-> runAction (action );
}