首页 > 代码库 > Cocos2d-x 多点触摸
Cocos2d-x 多点触摸
多点触摸的实现步骤与单点触摸类似,setTouchEnabled(true)开启触摸功能,注册触摸事件,重载多点触摸函数:ccTouchesBegan(开始)、ccTouchesMoved(移动)、ccTouchesEnded(结束)、ccTouchesCancelled(系统中断取消触摸事件),每个多点触摸函数都可以通过遍历整个CCSet来获得所有的触点。
程序代码:
头文件加入触摸重载函数:
virtual void registerWithTouchDispatcher(void); virtual void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent); virtual void ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent); virtual void ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent); virtual void ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent);
cpp文件加入:
setTouchEnabled(true);
定义实现这些函数:
void HelloWorld::registerWithTouchDispatcher(){ CCDirector *pdir = CCDirector::sharedDirector(); pdir->getTouchDispatcher()->addStandardDelegate(this, 0);}void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){ CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++) { CCTouch* pTouch = (CCTouch*)(*iter); CCPoint location = pTouch->getLocation(); CCLOG("ccTouchesBegan x: %f, y: %f", location.x, location.y); }}void HelloWorld::ccTouchesEnded(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){ CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++) { CCTouch* pTouch = (CCTouch*)(*iter); CCPoint location = pTouch->getLocation(); CCLOG("ccTouchesEnded x: %f, y: %f", location.x, location.y); }}void HelloWorld::ccTouchesMoved(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){ CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++) { CCTouch* pTouch = (CCTouch*)(*iter); CCPoint location = pTouch->getLocation(); CCLOG("ccTouchesMoved x: %f, y: %f", location.x, location.y); }}void HelloWorld::ccTouchesCancelled(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent){ CCSetIterator iter = pTouches->begin(); for (; iter != pTouches->end(); iter++) { CCTouch* pTouch = (CCTouch*)(*iter); CCPoint location = pTouch->getLocation(); CCLOG("x: %f, y: %f", location.x, location.y); }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。