In the cocos2d-x, you want to move an genie from its original position to the point the user is touched by, and rotate the radian in the direction of the genie, for more information, see my practice. Here, the genie uses a fish. After a user touches the fish, the fish is moved to the point touched, at the beginning of the Movement, the direction of the fish head has been directed to the touch point. The following is a detailed practice. First, the hfile declares to rewrite the four methods in the CCLayer: virtualvoid registerWithTouchDispatcher (void ); virtualbool compute (CCTouch * pTouch, CCEvent * pEvent); virtualvoid ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent); virtualvoid ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent ); next, let the init method in the cpp file touch the current layer: this-> setTouchEnabled (true); then rewrite the method in the cpp file:
Void StartGame: registerWithTouchDispatcher () {CCDirector: sharedDirector ()-> getTouchDispatcher ()-> addTargetedDelegate (this, INT_MIN-1, true);} bool StartGame :: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {CCPoint p = pTouch-> getLocation (); CCSprite * nowsprite = (CCSprite *) this-> getchildbytag( 888 ); // obtain my genie nowsprite-> cocos2d: CCNode: setRotation (atan2 (p. x-nowsprite-> getPositionX (), (p. y-nowsprite-> getPositionY () * 180/3. 1415926 + 90); // after changing the radians, add no 90. Based on the sprite's initial angle, What Is CCMoveTo * move_ten = CCMoveTo: create (1, p ); // set the time and coordinates for moving nowsprite-> runAction (move_ten); return true;} void StartGame: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {CCPoint p = pTouch-> getLocation (); CCSprite * nowsprite = (CCSprite *) this-> getChildByTag (888); nowsprite-> setPosition (p);} void StartGame :: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {// write the functions to be implemented for the end touch}