[Cocos2d-x] let the genie respond to touch and rotate the direction to the corresponding angle

Source: Internet
Author: User

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}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.