How to scale and solve problems of cocos2d-x multi-touch

Source: Internet
Author: User

# Ifndef _ loading _ MoreTouches __# define _ loading _ MoreTouches __# include <iostream> # include "cocos2d. h "USING_NS_CC; class MoreTouches: public CCLayer {public: bool init (); // virtual void registerWithTouchDispather (void); // because it is inherited from CCLayer, this method does not need to be rewritten, but the following methods need to be rewritten: virtual void ccTouchesCancellnd (CCSet * pTouches, CCEvent * pEvent); virtual void ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent); // note that the returned type of this method is different from that of the single-touch method. virtual void ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent); virtual void ccTouchesMoved, CCEvent * pEvent); static CCScene * scene (); virtual void onEnter (); virtual void onExit (); CREATE_FUNC (MoreTouches); public: double distance; // The distance between two touch points is double deltax; // The change value of the target X axis is double deltay; // The change value of the target Y axis is CCSprite * bg; // The target genie double mscale; // scale the initial map}; # endif/* defined (_ loading _ MoreTouches __)*/
# Include "MoreTouches. h" bool MoreTouches: init () {if (! CCLayer: init () {return false;} bg = CCSprite: create ("fullbg.png"); // initialize the target image this-> addChild (bg ); mscale = 1.0; // return true when initializing the scaling ratio of the image;} // void MoreTouches: registerWithTouchDispather () // {// CCDirector: shareddire()-> getTouchDispatcher () -> addStandardDelegate (this, 0); //} void MoreTouches: ccTouchesBegan (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent) {if (pTouches-> count ()> = 2) // if the touch point is no less than two {CCSetIterator iter = pTouches-> begin (); CCPoint mPoint1 = (CCTouch *) (* iter)-> getLocationInView (); mPoint1 = CCDirector: sharedDirector ()-> convertToGL (mPoint1); iter ++; CCPoint mPoint2 = (CCTouch *) (* iter)-> getLocationInView (); mPoint2 = CCDirector: sharedDirector ()-> convertToGL (mPoint2); distance = sqrt (mPoint2.x-mPoint1.x) * (mPoint2.x-mPoint1.x) + (mPoint2.y-mPoint1.y) * (mPoint2.y-mPoint1.y); // calculate the distance between two touch points: deltax = (mPoint1.x + mPoint2.x) /2-bg-> getPositionX (); // get the difference between the two touch points and the genie anchor. deltay = (mPoint1.y + mPoint2.y)/2-bg-> getPositionY (); CCLog ("ccTouchesBegan... ") ;}} void MoreTouches: ccTouchesMoved (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent) {if (pTouches-> count ()> = 2) // if the number of touch points to be moved is not less than two {CCSetIterator iter = pTouches-> begin (); CCPoint mPoint1 = (CCTouch *) (* iter )) -> getLocationInView (); mPoint1 = CCDirector: sharedDirector ()-> convertToGL (mPoint1); iter ++; CCPoint mPoint2 = (CCTouch *) (* iter )) -> getLocationInView (); mPoint2 = CCDirector: sharedDirector ()-> convertToGL (mPoint2 ); // obtain the distance between two new touch points. double mdistance = sqrt (mPoint1.x-mPoint2.x) * (mPoint1.x-mPoint2.x) + (mPoint1.y-mPoint2.y) * (mPoint1.y-mPoint2.y )); mscale = mdistance/distance * mscale; // new distance/old distance * Original scaling ratio, that is, the new scaling ratio distance = mdistance; bg-> setScale (mscale ); double x = (mPoint2.x + mPoint1.x)/2-deltax; // calculate the difference between the midpoint of the two contacts and the genie anchor. double y = (mPoint2.y + mPoint1.y)/2-deltay; bg-> setPosition (ccp (x, y); // keep the difference between the midpoint of the two contacts and the genie anchor. deltax = (mPoint1.x + mPoint2.x) /2-bg-> getPositionX (); // calculate the new offset deltay = (mPoint2.y + mPoint1.y)/2-bg-> getPositionY (); CCLog ("ccTouchMoved .... ");} if (pTouches-> count () = 1) // if the touch point is a {CCSetIterator iter = pTouches-> begin (); CCPoint mPoint = (CCTouch *) (* iter)-> getLocationInView (); mPoint = CCDirector: shareddire()-> convertToGL (mPoint ); // coordinates bg-> setPosition (mPoint); // directly move the genie} void MoreTouches: ccTouchesEnded (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent) {} void MoreTouches: Centers (cocos2d: CCSet * pTouches, cocos2d: CCEvent * pEvent) {} CCScene * MoreTouches: scene () {CCScene * scene = CCScene :: create (); MoreTouches * layer = MoreTouches: create (); scene-> addChild (layer); return scene;} void MoreTouches: onEnter () {CCLayer :: onEnter (); setTouchEnabled (true); CCLog ("onenter");} void MoreTouches: onExit () {CCLayer: onExit (); CCDirector: shareddire () -> getTouchDispatcher ()-> removeDelegate (this); // remove the touch proxy}

 

First, we need to implement the scaling logic. In cctouchesmovan, we can detect the number of touch points greater than two. If the number of touch points is greater than two, take the first two points, use the two-point distance formula to calculate the two points distance, and check in ccTouchesMoved, if the number of touch points is greater than two, continue to calculate the distance between the two points, and then use the distance to calculate the scaling ratio. Call setScale to set the scaling ratio. In addition to scaling, you also need to deal with location issues. In ccTouchesBegan, calculate the difference between the midpoint position and the spiritual anchpoint of the two contacts. In ccTouchesMoved, keep the difference between the two contact points and the genie anchor point unchanged, and do not need to be modified in ccTouchesEnded and ccTouchesCancelled. Next, let's focus on the problems encountered during implementation. The main one is that when I operate on the ios simulator, I press the option key to display two points, however, the program can always get only one vertex, that is, count = 1, which cannot be scaled at all. Later, Google finally came up with the result. It was necessary to modify the AppController. mm file under the ios directory.
Static AppDelegate s_sharedApplication;-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions {// Override point for customization after application launch. // Add the view controller's view to the window and display. window = [[UIWindow alloc] keys: [[UIScreen mainScreen] bounds]; EAGLView * _ glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: includepthformat: inclupreservebackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples: 0]; // Use RootViewController manage EAGLView viewController = [[RootViewController alloc] initWithNibName: nil bundle: nil]; viewController. wantsFullScreenLayout = YES; viewController. view = _ glView; // Set RootViewController to window if ([[UIDevice currentDevice]. systemVersion floatValue] <6.0) {// warning: addSubView doesn' t work on iOS6 [window addSubview: viewController. view];} else {// use this method on ios6 [window setRootViewController: viewController];} [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; cocos2d :: CCApplication: sharedApplication ()-> run (); [_ glView setMultipleTouchEnabled: YES]; // return YES ;}

 


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.