Coordinates in a Cocos2d-X
Coordinates in a Cocos2d-x can be divided into four types:
1. GL coordinate system: the lower left corner of the GL coordinate system is the coordinate origin, the X axis is right, and the Y axis is up.
2. UI coordinate system: the top left corner of the UI coordinate system is the coordinate origin, the X axis is right, and the Y axis is up.
3. World coordinate system: it is the coordinate system of the window. It is the GL coordinate system. It is the coordinate origin in the lower left corner. The X axis is right, and the Y axis is up.
4. Node coordinate system: it is a Node coordinate system, which is a GL coordinate system. Unlike the world coordinate system, its origin is the lower left corner of the Node,
When a node calls SetPosition, the parameter used is the coordinate system of its parent node (rendering tree ).
The size of the CCLayer is the same as that of the window by default, so its coordinate system and the world coordinate system overlap.
The default size of CCScene is the same as that of the window, so its coordinate system and world coordinates
When you create an Genie without setting the coordinates of the genie, the default coordinates of the genie are (0, 0), and the Node parent coordinate system is used.
Because the default size of the CCLayer is the same as that of the window, the coordinate system overlaps with the world coordinate system, so the Genie of the following code is located in the lower left corner.
// Initialize the genie bool Coord: init () {// call the init function CCLayer: init () of the parent class first; // create an genie CCSprite * sprite = CCSprite :: create ("CloseNormal.png"); addChild (sprite); return true ;}
Execution result:
Create an Genie and set the coordinates of the genie to (100,100). The default coordinates of the genie are (0, 0), and the parent node coordinate system is used. The reason is the same as above.
// Initialize the genie bool Coord: init () {// call the init function CCLayer: init () of the parent class first; // create an genie CCSprite * sprite = CCSprite :: create ("CloseNormal.png"); addChild (sprite); // set the sprite coordinates to (100,100), node coordinates sprite-> setPosition (ccp (100,100); return true ;}
Execution result:
Create a red background in the scenario and an genie on the background
// Initialize the genie bool Coord: init () {// call the init function CCLayer: init () of the parent class first; // obtain the window size CCSize winSize = CCDirector :: sharedDirector ()-> getWinSize (); // create a CCLayerColor * layerColor = CCLayerColor: create (ccc4 (255, 0, 0,255), winSize. width/2, winSize. height/2); addChild (layerColor); // create an genie CCSprite * sprite = CCSprite: create ("CloseNormal.png "); // Add the genie to the red background layerColor-> addChild (sprite); // set the coordinates of the genie to (100,100) (a node Coordinate System) sprite-> setPosition (ccp (100,100); return true ;}
Execution result
Program parsing: The above code first creates a red background, then adds an genie to the red background, and then sets the coordinates of the genie to (100,100). For the genie, the red background is equivalent to the parent node of the Genie. The coordinate origin is located in the lower left corner of the red background. Because the coordinate origin of the red background is the same as the coordinate origin of the window, the location of the genie remains unchanged.
Code after setting the coordinates of the red background to (100,100:
Bool Coord: init () {// call the init function CCLayer: init () of the parent class first; // obtain the window size CCSize winSize = CCDirector: shareddire () -> getWinSize (); // create a CCLayerColor * layerColor = CCLayerColor: create (ccc4 (255, 0, 0,255), winSize. width/2, winSize. height/2); addChild (layerColor); // create an genie CCSprite * sprite = CCSprite: create ("CloseNormal.png "); // Add the genie to the red background layerColor-> addChild (sprite); // set the coordinates of the genie to (100,100) (a node Coordinate System) sprite-> setPosition (ccp (100,100); // set the coordinates of the red background to (100,100) layerColor-> setPosition (ccp (100,100); return true ;}
Execution result:
Program parsing: in the Code, the coordinates of the red background and the genie are both (100,100). Because the red background is the parent class of the genie, the lower-left corner of the red background is the coordinate origin of the genie.
Coordinate Transformation in the program:
1. Conversion between UI coordinates and world coordinates
CCDirector: sharedDirector ()-> convertToUI ();
CCDirector: sharedDirector ()-> convertToGL ();
2. Conversion between world coordinates and node coordinates
CCNode: convertToNodeSpace (CCPoint ptInWorld );
CCNode: convertToWorldSpace (CCPoint ptInNode );
3. Conversion Method Based on the origin of the anchor point
CCNode: convertToNodeSpaceAR (CCPoint ptInWorld); the node coordinates returned by this function are origin points
CCNode: convertToWorldSpaceAR (CCPoint ptInNode );
4. Use touch for parameter conversion
CCNode: convertTouchToNodeSpace, which directly converts the touch to the coordinates of the node coordinate system.
Example: Convert the sprite node coordinates to the world coordinates
Program code:
Bool Coord: init () {// call the init function CCLayer: init () of the parent class first; // obtain the window size CCSize winSize = CCDirector: shareddire () -> getWinSize (); // create a CCLayerColor * layerColor = CCLayerColor: create (ccc4 (255, 0, 0,255), winSize. width/2, winSize. height/2); // Add the red background to the layer addChild (layerColor); // create a sprite CCSprite * sprite = CCSprite: create ("CloseNormal.png "); // Add the genie to the red background layerColor-> addChild (sprite); // set the coordinates of the genie to (100,100) (a node Coordinate System) sprite-> setPosition (ccp (100,100); // set the coordinates of the red background to (100,100) layerColor-> setPosition (ccp (100,100 )); // use ptSpriteInWorld to save the converted coordinates CCPoint ptSpriteInWorld = layerColor-> convertToWorldSpace (sprite-> getPosition ()); // print the converted world coordinates CCLog ("ptSpriteInWorld (% f, % f)", ptSpriteInWorld. x, ptSpriteInWorld. y); return true ;}
Execution Result: After compilation is successful, the red will show the converted coordinates (200,200) in the output)
Handles touch events and displays UI coordinates
Program code:
First, add the following code in Coord. h (Coord is a scenario created)
# Ifndef _ Coord_H _ # define _ Coord_H _ // prevents code re-inclusion # include "cocos2d. h "USING_NS_CC; class Coord: public CCLayer {public: // create a scenario static CCScene * scene (); // initialize the scenario bool init (); // menu callback function void menuCloseCallback (CCObject * pSender); // response to the touch Processing Event bool ccTouchBegan (CCTouch *, CCEvent *); CCSprite * _ sprite; // used to create a creation, Sprite, or layer (same as create () CREATE_FUNC (Coord);}; # endif
Then add the following code in Coord. cpp.
# Include "Coord. h "# include" HelloWorldScene. h "CCScene * Coord: scene () {CCScene * scene = CCScene: create (); CCLayer * layer = Coord: create (); scene-> addChild (layer); return scene;} bool Coord: init () {// call the init function of the parent class CCLayer: init (); // obtain the size of the window. CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // create a red background. CCLayerColor * layerColor = CCLayerColor :: create (ccc4 (255, 0, 0,255), winSize. width/2, winSize. height/2); // Add the red background to the layer addChild (layerColor); // create a sprite CCSprite * sprite = CCSprite: create ("CloseNormal.png "); // assign the sprite value to the global variable _ sprite = sprite; // Add the sprite to the red background layerColor-> addChild (sprite); // set the coordinates of the sprite to (100,100) (Is the node Coordinate System) sprite-> setPosition (ccp (100,100); // set the coordinates of the red background to (100,100) layerColor-> setPosition (ccp (100,100 )); // use ptSpriteInWorld to save the converted coordinates CCPoint ptSpriteInWorld = layerColor-> convertToWorldSpace (sprite-> getPosition ()); // print the converted world coordinates CCLog ("ptSpriteInWorld (% f, % f)", ptSpriteInWorld. x, ptSpriteInWorld. y); // Add a touch to demonstrate the UI coordinate and GL coordinate conversion. setTouchEnabled (true); setTouchMode (kCCTouchesOneByOne); return true ;} // response to the touch Processing Event bool Coord: ccTouchBegan (CCTouch * touch, CCEvent *) {// obtain the UI coordinate CCPoint ptInUI = touch-> getLocationInView () of the touch point (); // obtain the world coordinate CCPoint ptInGL = touch-> getLocation () of the touch point. // convert the UI coordinate to the GL coordinate CCPoint ptUIConvert = CCDirector: sharedDirector () -> convertToGL (ptInUI); // print the UI coordinate CCLog ("ptInUI (% f, % f)", ptInUI. x, ptInUI. y); // print the GL coordinate CCLog ("ptInGL (% f, % f), ptConvert (% f, % f)", ptInGL. x, ptInGL. y, ptUIConvert. x, ptUIConvert. y); // obtain the rectangle CCRect rc = _ sprite-> boundingBox (); // _ sprite-> getParent ()-> convertToNodeSpace (touchPoint) convert touch to coordinate system coordinate points of the parent node of the genie // if (rc. containsPoint (_ sprite-> getParent ()-> convertToNodeSpace (touchPoint) if (rc. containsPoint (_ sprite-> getParent ()-> convertTouchToNodeSpace (touch) {CCLog ("Oh I am touched");} return true ;}
Execution result:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MaGitbHK87HqteO797rsyau + 2 NDOv/LW0LXEvqvB6cqxKMzhyr60pcP + climate = "http://www.2cto.com/uploadfile/Collfiles/20140930/2014093009203310.png" alt = "\">
2. When the mouse does not click the genie