微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Cocos2d-x之Touch事件处理机制

http://blog.linguofeng.com/archive/2012/09/12/cocos2d-x-touch.html

提供两种触摸事件处理机制:CCStandardTouchDelegate和CCTargetedTouchDelegate。


CCStandardTouchDelegate认事件
virtual void cctouchesBegan(CCSet *ptouches,CCEvent *pEvent); 处理按下事件
virtual void cctouchesMoved(CCSet *ptouches,221)"> 处理按下并移动事件
virtual void cctouchesEnded(CCSet *ptouches,221)"> 处理松开事件
virtual void cctouchesCancelled(CCSet *ptouches,221)"> 处理打断事件
CCTargetedTouchDelegate
virtual bool ccTouchBegan(CCTouch *pTouch,221)"> 处理用户按下事件,true表示继续处理,否则false.
virtual void ccTouchMoved(CCTouch *pTouch,232)">virtual void ccTouchEnded(CCTouch *pTouch,232)">virtual void ccTouchCancelled(CCTouch *pTouch,221)"> 处理打断事件

两者的区别:CCSetCCTouch一个事件集合一个单个事件。

事件分发的顺序:CCTargetedTouchDelegateCCStandardTouchDelegate

认情况下所有cclayer都没有启用触摸事件,需要this->setIsTouchEnabled(true);启用。

如需更改事件:void registerWithTouchdispatcher(void) {}

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}

利用ccTouchBegancctouchesBegan加以实现点击的回调

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// 不执行}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐