CCTouch继承CCObject
/** returns the current touch location in OpenGL coordinates */
CCPoint getLocation() const;
/** returns the previous touch location in OpenGL coordinates */
CCPoint getPreviousLocation() const;
/** returns the start touch location in OpenGL coordinates */
CCPoint getStartLocation() const;
/** returns the delta of 2 current touches locations in screen coordinates */
CCPoint getDelta() const;
/** returns the current touch location in screen coordinates */
CCPoint getLocationInView() const;
/** returns the previous touch location in screen coordinates */
CCPoint getPreviousLocationInView() const;
/** returns the start touch location in screen coordinates */
CCPoint getStartLocationInView() const;
// returns the current touch location in screen coordinates
CCPoint CCTouch::getLocationInView() const
{
return m_point;
}
// returns the previous touch location in screen coordinates
CCPoint CCTouch::getPreviousLocationInView() const
{
return m_prevPoint;
}
// returns the start touch location in screen coordinates
CCPoint CCTouch::getStartLocationInView() const
{
return m_startPoint;
}
// returns the current touch location in OpenGL coordinates
CCPoint CCTouch::getLocation() const
{
return CCDirector::sharedDirector()->convertToGL(m_point);
}
// returns the previous touch location in OpenGL coordinates
CCPoint CCTouch::getPreviousLocation() const
{
return CCDirector::sharedDirector()->convertToGL(m_prevPoint);
}
// returns the start touch location in OpenGL coordinates
CCPoint CCTouch::getStartLocation() const
{
return CCDirector::sharedDirector()->convertToGL(m_startPoint);
}
// returns the delta position between the current location and the previous location in OpenGL coordinates
CCPoint CCTouch::getDelta() const
{
return ccpSub(getLocation(), getPreviousLocation());
}
//test the function()
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
CCPoint point1 = pTouch->getLocation();
CCPoint point2 = pTouch->getLocationInView();
CCPoint point3 = pTouch->getPreviousLocation();
CCPoint point4 = pTouch->getPreviousLocationInView();
CCPoint point5 = pTouch->getStartLocation();
CCPoint point6 = pTouch->getStartLocationInView();
CCPoint point7 = pTouch->getDelta();
int id = pTouch->getID();
CCLog("getLocation:%f---%f",point1.x,point1.y);
CCLog("getLocationInView:%f---%f",point2.x,point2.y);
CCLog("getPreviousLocation:%f---%f",point3.x,point3.y);
CCLog("getPreviousLocationInView:%f---%f",point4.x,point4.y);
CCLog("getStartLocation:%f---%f",point5.x,point5.y);
CCLog("getStartLocationInView:%f---%f",point6.x,point6.y);
CCLog("getDelta:%f---%f",point7.x,point7.y);
CCLog("getID:%d",id);
}
ccTouchBegan 和 ccTouchEnded 在同一个点,发现3组数据都一样
Cocos2d: getLocation:148.505676---552.673523
Cocos2d: getLocationInView:148.000000---87.000000
Cocos2d: getPreviousLocation:148.505676---552.673523
Cocos2d: getPreviousLocationInView:148.000000---87.000000
Cocos2d: getStartLocation:148.505676---552.673523
Cocos2d: getStartLocationInView:148.000000---87.000000
Cocos2d: getDelta:0.000000---0.000000
Cocos2d: getID:0
ccTouchBegan 和 ccTouchEnded 不在同一个点,开始点在(4.863325---7.565172),最终点在(667.802612---303.092102)
Cocos2d: getLocation:667.802612---303.092102
Cocos2d: getLocationInView:668.000000---337.000000
Cocos2d: getPreviousLocation:667.802612---303.092102
Cocos2d: getPreviousLocationInView:668.000000---337.000000
Cocos2d: getStartLocation:4.863325---7.565172
Cocos2d: getStartLocationInView:4.000000---633.000000
Cocos2d: getDelta:0.000000---0.000000
Cocos2d: getID:0
getLocation:
CCDirector::sharedDirector()->convertToGL(m_point);
//获取在opengl坐标系中的坐标
getLocationInView
//获取屏幕坐标
getPreviousLocation:
/获取上一次在opengl坐标系中的位置
getPreviousLocationInView:
//获取上一次的屏幕坐标
getStartLocation:开始点的opengl坐标系中得位置
getStartLocationInView:开始点的屏幕坐标
getDelta:ccpSub(getLocation(), getPreviousLocation())