首页 > 代码库 > cocos2d-x 多触点监听
cocos2d-x 多触点监听
[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 多触点监听
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。