Cocos2d-x之Touch事件处理机制
发布时间:2020-12-14 21:29:27 所属栏目:百科 来源:网络整理
导读:http://blog.linguofeng.com/archive/2012/09/12/cocos2d-x-touch.html 提供两种触摸事件处理机制:CCStandardTouchDelegate和CCTargetedTouchDelegate。 一、两种机制的四种不同的事件 CCStandardTouchDelegate 默认事件 virtual void ccTouchesBegan(CCSet
http://blog.linguofeng.com/archive/2012/09/12/cocos2d-x-touch.html 提供两种触摸事件处理机制:CCStandardTouchDelegate和CCTargetedTouchDelegate。 一、两种机制的四种不同的事件
两者的区别: 事件分发的顺序: 默认情况下所有 如需更改事件: class MyLayer: public cocos2d:CCLayer { public virtualvoid registerWithTouchDispatcher(void); // addStandardDelegate() ccTouchesBegan(CCSet*pTouches,CCEventpEvent ccTouchesMoved ccTouchesEnded ccTouchesCancelled// addTargetedDelegate()bool ccTouchBeganCCTouchpTouch ccTouchMoved ccTouchEnded ccTouchCancelled } ::registerWithTouchDispatcher) // 委托,优先级 CCTouchDispatchersharedDispatcher()->addStandardDelegatethis kCCMenuTouchPriority// 委托,优先级,是否继续处理addTargetedDelegatetrue// 2.0版本以后CCDirectorsharedDirectorgetTouchDispatcher kCCMenuHandlerPriority} 二、 如果实现回调函数?利用 ccTouchesBegan// 单点pTouch =CCTouch*)(->anyObject());// 所有点forCCSetIterator iterTouch pTouches->begin();!=end iterTouch++) pCurTouch *)(*iterTouch // 获取点在视图中的坐标(左上角为原点)CCPoint touchLocation pTouchgetLocationInView// 把点的坐标转换成OpenGL坐标(左下角为原点) touchLocation convertToGL(touchLocation// 把OpenGL的坐标转换成CCLayer的坐标local convertToNodeSpace// 大小为100x100,坐标为(0,0)的矩形CCRect rect CCRectMake(0 100// 判断该坐标是否在rect矩形内 flag rect.containsPointlocalifflag // 回调else// 不执行} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |