cocos2d-x学习之双点触控的缩小与放大2

来源:互联网 发布:painter mac教程 编辑:程序博客网 时间:2024/06/16 06:30

学习多点触控的时候找了许多的资料参考,经过自己几天的摸索总算做出来了 。现在分享出来以供初学者参考  如有错误之处还请指出见谅 谢谢



先给效果图


首先我们要开启多点触控

在 AppController中加入

[__glView setMultipleTouchEnabled:YES];boolHelloWorld::init(){    if ( !CCLayer::init() )    {        returnfalse;    }    CCMenuItemImage *pCloseItem =CCMenuItemImage::create("CloseNormal.png","CloseSelected.png",this,                                                         menu_selector(HelloWorld::menuCloseCallback) );    pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width -20, 20) );        // create menu, it's an autorelease object    CCMenu* pMenu =CCMenu::create(pCloseItem, NULL);    pMenu->setPosition(CCPointZero );    this->addChild(pMenu,1);        // create and initialize a label    CCLabelTTF* pLabel =CCLabelTTF::create("双点触摸之精灵的缩小与放大","Arial", 24);    // ask director the window size    CCSize size =CCDirector::sharedDirector()->getWinSize();        pSprite =CCSprite::create("grossinis_sister1.png");    pSprite->setPosition(ccp(size.width/2, size.height/2) );    pLabel->setPosition(ccp(size.width/2,size.height/2+120));    this->addChild(pSprite,0,1);    this->addChild(pLabel);    this->setTouchEnabled(true);        returntrue;}初始化代码很容易懂的 就不多说了接下来我们看看重点cctouchesmove()void HelloWorld::ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent){        int i = 0;    CCPoint location;    CCSetIterator iter = pTouches->begin();    for (; iter != pTouches->end(); iter++)    {        i++;        CCTouch* pTouch = (CCTouch*)(*iter);        location = pTouch->locationInView();        location = CCDirector::sharedDirector()->convertToGL(location);        a[i] = location.x;    }//遍历取出每个触摸点坐标    begincout = abs(a[1] -a[2]);//判断起始两个点的x轴坐标并取绝对值得到其距离  }void HelloWorld::ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent){    CCPoint location;    CCSetIterator iter = pTouches->begin();    int movedx[3];    int i = 0;    for (; iter != pTouches->end(); iter++)    {        CCTouch* pTouch = (CCTouch*)(*iter);        location = pTouch->locationInView();        location = CCDirector::sharedDirector()->convertToGL(location);        i++;        movedx[i] = location.x;//取出x轴坐标    }        CCLog("%d",i);        int endcout = abs(movedx[1]-movedx[2]);//判断结束两个点的x轴坐标并取绝对值得到其距离    int cout = endcout-begincout;//判断手势是合拢还是分开        if(cout>0){            CCLog("手势打开");            CCRect rect=pSprite->boundingBox();                          if ((CCRect::CCRectContainsPoint(rect,location))) //判断触摸点是否在精灵上            {                                  CCFiniteTimeAction *action =CCScaleTo::create(3.0f,3);//精灵放大到原来的三倍                pSprite->runAction(action);                            }                    }else{            CCLog("手势合拢");            CCFiniteTimeAction *action =CCScaleTo::create(3.0f,1);//精灵缩小回原来大小            pSprite->runAction(action);       }}



Ok  今天就到此吧 玩游戏了  有什么指教和疑问请留言谢谢!