COCOS-3.X事件分发机制-触摸事件

COCOS-3.X事件分发机制-触摸事件,第1张

概述在cocos中,触摸事件分为单点触摸和多点触摸,其中多点触摸主要是针对移动设备的,但是利用cocos的事件分发机制(点击打开链接)来处理触摸事件,其大致流程几乎是一致的。 一、单点触摸 在以前cocos2.x版本中,触摸事件的响应主要是通过重写父类中关于响应触摸事件的虚函数来实现的: // 需要重写的函数,单数形式bool ccTouchBegan(CCTouch *pTouch, CCEven

在cocos中,触摸事件分为单点触摸和多点触摸,其中多点触摸主要是针对移动设备的,但是利用cocos的事件分发机制(点击打开链接)来处理触摸事件,其大致流程几乎是一致的。

一、单点触摸

在以前cocos2.x版本中,触摸事件的响应主要是通过重写父类中关于响应触摸事件的虚函数来实现的:

// 需要重写的函数,单数形式bool cctouchBegan(CCtouch *ptouch,CCEvent *pEvent);    // 触摸开始,注意返回类型,true表示继续响应CCtouchmove,CCtouchend,CCtouchCancalled,false表示不响应voID cctouchmoved(CCtouch *ptouch,CCEvent *pEvent);    // 触摸滑动                              voID cctouchended(CCtouch *ptouch,CCEvent *pEvent);    // 触摸结束                  voID cctouchCancelled(CCtouch *ptouch,CCEvent *pEvent);// 触摸取消  例如中途来点// 然后需要注册触摸CCDirector::sharedDirector()->gettouchdispatcher()->addTargetedDelegate(this,true);// 最后需要移除触摸// CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this);

而在现在cocos2dx3.x版本中,触摸事件的响应改用了新的事件分发机制来实现:

/** 	 * 单点触摸	 */	// 创建单点触摸事件订阅者	auto touchListenerOne = EventListenertouchOneByOne::create();	// 设置事件触摸回调	touchListenerOne->ontouchBegan = CC_CALLBACK_2(touchEvent_Dome::ontouchBegan,this);	touchListenerOne->ontouchmoved = CC_CALLBACK_2(touchEvent_Dome::ontouchmoved,this);	touchListenerOne->ontouchended = CC_CALLBACK_2(touchEvent_Dome::ontouchended,this);	touchListenerOne->ontouchCancelled = CC_CALLBACK_2(touchEvent_Dome::ontouchCancelled,this);	// 向事件监听器提交单点触摸事件订阅	_eventdispatcher->addEventListenerWithSceneGraPHPriority(touchListenerOne,this);	// 另一种提交事件订阅的方式	// Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(touchListener1,this);// 移除所有与当前节点相关的事件订阅// _eventdispatcher->removeEventListenersForTarget(this);
  

一、多点触摸

多点触摸无论是在2.x版本,还是3.x版本下,实现原理和形式都是和对应版本下的单点触摸响应一致的,仅仅是名称上有所差异。

cocos2.x版本的多点触摸处理:

// 需要重写的函数, 复数形式voID cctouchesBegan(CCSet *ptouches,CCEvent *pEvent);  // 注意返回类型是voIDvoID cctouchesMoved(CCSet *ptouches,CCEvent *pEvent);voID cctouchesEnded(CCSet *ptouches,CCEvent *pEvent);// 然后需要注册触摸CCDirector::sharedDirector()->gettouchdispatcher()->addStandardDelegate( this,0);// 最后需要移除触摸// CCDirector::sharedDirector()->gettouchdispatcher()->removeDelegate(this);

cocos3.x版本的多点触摸处理:

/**	 * 多点触摸	 */	auto touchListenerAll = EventListenertouchAllAtOnce::create();	touchListenerAll->ontouchesBegan = CC_CALLBACK_2(touchEvent_Dome::ontouchesBegan,this);	touchListenerAll->ontouchesMoved = CC_CALLBACK_2(touchEvent_Dome::ontouchesMoved,this);	touchListenerAll->ontouchesEnded = CC_CALLBACK_2(touchEvent_Dome::ontouchesEnded,this);	touchListenerAll->ontouchesCancelled = CC_CALLBACK_2(touchEvent_Dome::ontouchesCancelled,this);	_eventdispatcher->addEventListenerWithSceneGraPHPriority(touchListenerOne,this);// 移除所有与当前节点相关的事件订阅// _eventdispatcher->removeEventListenersForTarget(this);
总结

以上是内存溢出为你收集整理的COCOS-3.X事件分发机制-触摸事件全部内容,希望文章能够帮你解决COCOS-3.X事件分发机制-触摸事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1075435.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存