:
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);//re-set the sprite coordinate when moving 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);F (location.x > 0 && location.x <960 &&location.y >0 && LOCATION.Y < 640)//Touch rectangular Area {ISC Ontrol = 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;} Register 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 ();}