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)。