其实cocos2d-x的多点触摸事件可以实现很多功能:譬如缩放 和简单的手势识别
在windows7 或者在OS X平台中暂时还不支持多点触控 请将项目部署到windows8,ios,或者安卓平台中
既然用到多点触控 不管什么功能 肯定要在初始化函数中 设置SetTouchEnable(true) 之后便是重载registerWithTouchDispatcher等五个函数
实现缩放功能:
- voidHelloWorld::registerWithTouchDispatcher(void)
- {
- CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
- }
- voidHelloWorld::ccTouchesBegan(CCSet*pTouches,CCEvent*pEvent)
- {
- if(pTouches->count()>=2){
- CCSetIteratoriter=pTouches->begin();
- CCPointmPoint1=((CCTouch*)(*iter))->locationInView();
- mPoint1=CCDirector::sharedDirector()->convertToGL(mPoint1);
- iter++;
- CCPointmPoint2=((CCTouch*)(*iter))->locationInView();
- mPoint2=CCDirector::sharedDirector()->convertToGL(mPoint2);
- distance=sqrt((mPoint1.x-mPoint2.x)*(mPoint1.x-mPoint2.x)+(mPoint1.y-mPoint2.y)*(mPoint1.y-mPoint2.y));
- deltax=(mPoint1.x+mPoint2.x)/2-pSprite->getPositionX();
- deltay=(mPoint1.y+mPoint2.y)/2-pSprite->getPositionY();
- }
- voidHelloWorld::ccTouchesMoved(CCSet*pTouches,248)"> floatmdistance=sqrt((mPoint1.x-mPoint2.x)*(mPoint1.x-mPoint2.x)+(mPoint1.y-mPoint2.y)*(mPoint1.y-mPoint2.y));
- mscale=mdistance/distance*mscale;
- distance=mdistance;
- pSprite->setScale(mscale);
- floatx=(mPoint1.x+mPoint2.x)/2-deltax;
- floaty=(mPoint1.y+mPoint2.y)/2-deltax;
- pSprite->setPosition(ccp(x,y));
- deltax=(mPoint1.x+mPoint2.x)/2-pSprite->getPositionX();
- deltay=(mPoint1.y+mPoint2.y)/2-pSprite->getPositionY();
- voidHelloWorld::ccTouchesEnded(CCSet*pTouches,CCEvent*pEvent)
- voidHelloWorld::ccTouchesCancelled(CCSet*pTouches,248)"> }
copy