Reprint Please specify the Source: http://blog.csdn.net/oyangyufu/article/details/25656673
:
The Cctouch class loads the information for the touch point. Contains the horizontal ordinate value of the touch point and the ID number of the touch point, such as getting the touch point to GL Coordinates:
Ccpoint point = Ptouch->getlocationinview ();p oint = Ccdirector::shareddirector ()->converttogl (point);
Create Touch Event Flow: First turn on settouchenabled (true), then override Registerwithtouchdispatcher call Touch Proxy function Addtargeteddelegate consent to the scene layer receiving touch events, Rewrite the Cctouchbegan, cctouchmoved, cctouchended, cctouchcancelled functions
Program code:
BOOL Helloworld::init () {////////////////////////////////1. Super init first if (! Cclayer::init ()) {return false; } ccsize visiblesize = Ccdirector::shareddirector ()->getvisiblesize (); Ccpoint origin = Ccdirector::shareddirector ()->getvisibleorigin (); 2. Add a menu item with "X" image, which are clicked to quit the program//may modify it. Add a "Close" icon to exit the progress. It ' s an Autorelease object ccmenuitemimage *pcloseitem = Ccmenuitemimage::create ( "Closenormal.png", "closeselected.png", th IS, 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 (+)); This->addchild (SP1); return true;} Touch move 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);//When moving, set the sprite coordinate again float x = location.x-deltax;float y = Location.y-deltay;sp1->setposition (CCP (x, Y));}} Touch Start Click to calculate the point coordinates with the sprite coordinate difference bool Helloworld::cctouchbegan (cctouch* Touch, ccevent* event) {Ccpoint pos = sp1-> GetPosition (); Ccpoint location = Touch->getlocationinview (), location = Ccdirector::shareddirector ()->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)//touch the rectangular area {is Control = true;//calculates the coordinate difference between the touch point and the 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;} Brochure Touch event void Helloworld::onenter () {ccdirector* pdirector = Ccdirector::shareddirector ();pD irector-> Gettouchdispatcher ()->addtargeteddelegate (this, 0, true); Cclayer::onenter ();} void Helloworld::onexit () {ccdirector* pdirector = Ccdirector::shareddirector ();pD irector->gettouchdispatcher ()- >removedelegate (this); Cclayer::onexit ();}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Cocos2d-x Single Touch--the genie that lets us move with our fingers