cocos2d-x 多触点监听

/首先到cocos2d-x项目下的ios文件夹下,找到AppController.mm文件,在函数 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 中添加如下函数:  [__glView setMultipleTouchEnabled:YES];  

  

bool HelloWorld::init()  

{  

      

    if ( !CCLayer::init() )  

    {  

        return false;  

    }  

      

    //开启多触点监听务必调用此函数  

    setTouchEnabled(true);  

      

    CCSprite* sp1 = CCSprite::create("Icon.png");  

    sp1->setPosition(ccp(150, 200));  

    addChild(sp1, 0, 23);  

      

    CCSprite* sp2 = CCSprite::create("Icon.png");  

    sp2->setColor(ccc3(0, 255, 0));  

    sp2->setPosition(ccp(150, 100));  

    addChild(sp2, 0, 24);  

      

    return true;  

}  

  

//第一次碰触  

void HelloWorld::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *event)  

{  

    CCSetIterator inter = touches->begin();  

    for(; inter != touches->end(); inter++)  

    {  

        CCTouch* touch = (CCTouch*)(*inter);  

        CCPoint point = touch->getLocation();  

        if(touch->getID() == 0) //第一个触点  

        {  

            CCSprite* sp1 = (CCSprite*)getChildByTag(23);  

            sp1->setPosition(point);  

        }else if(touch->getID() == 1)//第二个触点  

        {  

            CCSprite* sp2 = (CCSprite*)getChildByTag(24);  

            sp2->setPosition(point);  

        }  

    }  

}  

  

//移动或拖拽  

void HelloWorld::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *event)  

{  

    CCSetIterator inter = touches->begin();  

    for(; inter != touches->end(); inter++)  

    {  

        CCTouch* touch = (CCTouch*) (*inter);  

        CCPoint point = touch->getLocation();  

        if(touch->getID() == 0)  

        {  

            CCSprite* sp1 = (CCSprite*)getChildByTag(23);  

            sp1->setPosition(point);  

        }else if(touch->getID() == 1)  

        {  

            CCSprite* sp2 = (CCSprite*)getChildByTag(24);  

            sp2->setPosition(point);  

        }  

    }  

}  

  

//用户手指抬起  

void HelloWorld::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *event)  

{  

      

}  

  

//多触点的委托监听注册放在onEnter的生命函数中会造成程序异常退出。默认都写在下面函数中。  

void HelloWorld::registerWithTouchDispatche()  

{  

    CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);  

}  

  

  

//删除多触点的委托监听  

void HelloWorld::onExit()  

{  

    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);  

  

    //这句务必要写  

    CCLayer::onExit();  

}  

 

你可能感兴趣的:(cocos2d-x)