cocos2d-x 多觸點監聽
阿新 • • 發佈:2017-07-05
eve size inline let shared blog pbo har strong
[cpp] view
plaincopy
- //首先到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 多觸點監聽