Cocos2d-x single touch-let the genie move with your fingers

Source: Internet
Author: User

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 ();}



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.