coco2dx中屏幕坐标系是以左上角为原点的坐标系,而常用的openGL坐标系是右手坐标系,简单来说就是左下角为原点,所以在接收触摸事件时需要使用convertToGL使得触点坐标转为为openGL坐标方便使用。
convertToNodeSpace,我一开始同时接触这两个关键字的时候以为这是openGL的反向操作……看完说明才知道这是将一个坐标点转化到一个图层的坐标系中。
CCPoint touchLocation = pTouch->getLocationInView();//获得触点坐标
touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);//将触点坐标转化为GL坐标
touchLoctaion = pLayer->convertToNodeSpace(touchLocation);//将touchLocation转化为pLayer里的坐标。
有必要的话还需了解cocos2dx的屏幕坐标系统。
convertToNodeSpace可以在新建坐标系上进行坐标的转换。
pLayer.anchorPoint = CGPointMake(0,0);
touchLocation = pLayer->convertToNodeSpace(touchLoction);
旧触点(a, b)转化为新触点(a-x, b-y)。