Reprinted please indicate the source:Http://blog.csdn.net/oyangyufu/article/details/25656673
:
The CCTouch class loads the touch point information, including the touch point's X-axis and Y-axis values and the touch point's ID, such as obtaining the GL coordinates from the touch point:
CCPoint point = pTouch->getLocationInView();point = CCDirector::sharedDirector()->convertToGL(point);
Create a touch event process: first enable setTouchEnabled (true), then rewrite registerWithTouchDispatcher to call the touch proxy function addTargetedDelegate to allow the set layer to receive touch events, and then rewriteCcTouchBegan, ccTouchMoved, ccTouchEnded, and ccTouchCancelled Functions
Program code:
Bool HelloWorld: init () {// 1. super init first if (! CCLayer: init () {return false;} CCSize visibleSize = CCDirector: sharedDirector ()-> getVisibleSize (); CCPoint origin = CCDirector: shareddire () -> getVisibleOrigin (); // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelpolicobject CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("example", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback )); pCloseItem-> setPosition (ccp (origin. x + visibleSize. width-pCloseItem-> getContentSize (). width/2, origin. y + pCloseItem-> getContentSize (). height/2); // create menu, it's an autorelease object CCMenu * pMenu = CCMenu: create (pCloseItem, NULL); pMenu-> setPosition (CCPointZero ); this-> addChild (pMenu, 1); setTouchEnabled (true); sp1 = CCSprite: create ("cpp1.png"); sp1-> setScale (0.5f ); sp1-> setPosition (ccp (100,200); this-> addChild (sp1); return true;} // touch mobile void HelloWorld: ccTouchMoved (CCTouch * touch, CCEvent * event) {if (iscontrol) {CCPoint location = touch-> getLocationInView (); location = CCDirector: sharedDirector ()-> convertToGL (location ); CCLOG ("ccTouchMoved... x: % f y: % f ", location. x, location. y); // reset the sprite coordinate float x = location when moving. x-deltax; float y = location. y-deltay; sp1-> setPosition (ccp (x, y) ;}}// click the start point to calculate the bool HelloWorld :: ccTouchBegan (CCTouch * touch, CCEvent * event) {CCPoint pos = sp1-> getPosition (); CCPoint location = touch-> getLocationInView (); location = CCDirector: shareddire () -> convertToGL (location); // openGLCCLOG ("ccTouchBegan... x: % f y: % f ", location. x, location. y); if (location. x> 0 & location. x <960 & location. y> 0 & location. y <640) // The rectangular area of the touch {iscontrol = true; // calculates the Coordinate Difference Between the touch point and sprite deltax = location. x-pos.x; deltay = location. y-pos.y;} return true;} // touch end void HelloWorld: ccTouchEnded (CCTouch * touch, CCEvent * event) {CCLOG ("ccTouchEnded... "); // iscontrol = false;} // register the touch event void HelloWorld: onEnter () {CCDirector * pDirector = CCDirector: sharedDirector (); pDirector-> getTouchDispatcher () -> addTargetedDelegate (this, 0, true); CCLayer: onEnter ();} void HelloWorld: onExit () {CCDirector * pDirector = CCDirector: shareddire (); pDirector-> getTouchDispatcher ()-> removeDelegate (this); CCLayer: onExit ();}