A friend admitted to the flight attendant. She said: I will not be back in the future. Please call me Bai fumei at the next meeting! Although a little envious, I still do not want to show weakness and reply: Please call me Gao fushuai next time!
Who can say this in the future?
------------------
I haven't used onTouchBegan for a while. Recently, I found that convertToGL () and getLocation () interfaces are a little unfamiliar when I write code. So I took this opportunity to write a summary to my blog, next time I forget to go to my blog and take notes.
First, fill in the basic knowledge:
1. OpenGL coordinate system: the origin of the coordinate system is in the lower left corner of the screen, with the X axis being right and the Y axis being up. This is the coordinate system used in cocos2dx.
2. Screen coordinate system: the origin of the coordinate system is in the upper left corner of the screen, the X axis is right, and the Y axis is down. In fact, the difference between the X axis and the OpenGL coordinate system is that the Y axis is pulled in the direction. Assume that the resolution of a game scenario is (500,500), and the coordinate of a point is (200,200), the coordinates in the OpenGL coordinate system are (200,200), and the coordinates in the screen coordinate system are reversed, is (200,500-200 ). Actually, there is a difference between 6 and 9...
3. World coordinate system: it is also called absolute coordinate system. We only need to know that the world coordinate system is consistent with the OpenGL coordinate system. The origin is in the lower left corner of the screen, and the X axis is right and the Y axis is up.
4. node Coordinate System: Also known as relative coordinate system, which is in the same direction as OpenGL coordinate system. The difference is that the origin is in the lower left corner of the parent node.
The following is an example:
1. Let's talk about the OpenGL coordinate system and the screen coordinate system first.
Didn't I mention onTouchBegan (Touch * touch, Event * event,The contact coordinate sent from the touch parameter is the screen coordinate system.To obtain the coordinate system:
Auto point = touch-> getLocationInView (); // obtain the screen Coordinate System
Of course, we
Generally, you should convert the screen coordinate system to the OpenGL coordinate system.There are two methods:
Auto point = touch-> getLocation (); // get it directly from touch. In the getLocation () source code, the coordinates are converted into OpenGL Coordinate System auto point = touch-> getLocationInView (); point = Director: getInstance ()-> convertToGL (point); // obtain the screen coordinate first and convert it to the OpenGl coordinate system by calling convertToGL.
The complete code is as follows:
Bool HelloWorld: onTouchBegan (Touch * touch, Event * event) {auto point = touch-> getLocation (); // obtain the OpenGl coordinate system CCLOG ("Location point x = % f, y = % f ", point. x, point. y); auto point2 = touch-> getLocationInView (); // obtain the screen coordinate CCLOG ("LocationInView point x = % f, y = % f", point2.x, point2.y ); point2 = Director: getInstance ()-> convertToGL (point2); // convert the screen coordinates to OpenGL coordinates CCLOG ("convertToGL1 point x = % f, y = % f ", point2.x, point2.y); point2 = Director: getInstance ()-> convertToGL (point2); // pay attention to this CCLOG ("convertToGL2 point x = % f, y = % f ", point2.x, point2.y); return true ;}
The resolution of my current screen is:960x640. The output result is as follows:
Is there an interesting phenomenon? convertToGL converts the point screen coordinate to the OpenGL coordinate, and after convertToGL is used for the point, the point is converted from the OpenGL coordinate to the screen coordinate, haha.
2. Let's talk about the node coordinates. The world coordinates are not going to be described in detail, because I have never used them any more...
A typical usage of node coordinates should be scrollView. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples/examples/ru/examples/ycrHw7 + examples + xOPDv7TOu/G1w7XE1/examples + examples/PPwr3Ho6y2 + examples/examples + tfFuMSx5KGjveK + 9rDst6i + examples LHq16qzyb3atePX + LHqo6y3vbeoyOfPwqO6PGJyIC8 + PHByZSBjbGFzcz0 = "brush: java; "> point = scroll_layer-> convertToNodeSpace (point); // assume that the layer in scrollView is scroll_layer. Speaking of scrollView, do you want to talk about scrollView skills in the next article?
Well, let's do it first. Every time I write a blog, it's hard to get started. In fact, it's only 11 o'clock ~
Respect Original, reprinted please indicate Source: http://blog.csdn.net/star530/article/details/25250031