cocos2dx CCLayer触屏事件研究

原作者 herain_cs,原文链接 http://blog.csdn.net/herain_cs/article/details/8627995

对于CCLayer子类,想响应触屏事件比较简单,init的时候调用setTouchEnable并重载下列函数:

[cpp]  view plain copy
  1. virtual void registerWithTouchDispatcher(void);  
  2. virtual bool ccTouchBegan(CCTouch *pTouch,CCEvent *pEvent);  
  3. virtual void ccTouchMoved(CCTouch *pTouch,CCEvent *pEvent);  
  4. virtual void ccTouchEnded(CCTouch *pTouch,CCEvent *pEvent);  
  5. virtual void ccTouchCancelled(CCTouch *pTouch,CCEvent *pEvent);  


典型的registerWithTouchDispatcher实现是:

[cpp]  view plain copy
  1. void XXLayer::registerWithTouchDispatcher()  
  2. {  
  3.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);  
  4. }  


做的事情就是注册触屏事件。

addTargetedDelegate的第一个参数是delegate,就是回调函数执行的对象;第二个参数priority和第三个参数bSwallowsTouches决定多个对象同时注册触屏事件时的行为。具体的意义是:

priority值越小,回调越早被调用。

bSwallowsTouches为true,则ccTouchBegan返回ture以后,其他的回调将不再被调用,就好像触屏事件被吃掉了一样。


ccTouchxx系列函数就是触屏事件的响应函数。


CCLayer提供了一个registerWithTouchDispatcher的默认实现,主要功能相当于

CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0)

也是注册触屏事件。但是addStandardDelegate和前面我们使用的addTargetedDelegate有诸多不同。

1.不支持bSwallowsTouches参数,在触屏动作发生时,所有注册了触屏事件的对象的回调函数都会被调用。

2.回调函数定义不同,它的回调函数定义是:

    virtualvoid ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);

    virtualvoid ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);

    virtualvoid ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);

    virtualvoid ccTouchesCancelled(CCSet *pTouches,CCEvent *pEvent);


回调函数的第一个参数是CCSet*,另外ccTouchesBegan也不像ccTouchBegan那样有一个bool型的返回值。

网上有人提到支持多点触摸时需要使用addStandardDelegate,暂时还没有研究过。


前面只说了注册事件,没有提注销。CCLayer是在onExit中帮我们做了这件事,子类就不用再处理了。


下面提供一个CCLayer子类响应触屏事件的实例,用cocos2dx template生成Hello world工程后,替换HelloWorldScene.h和HelloWorldScene.cpp即可测试这个例子,这个例子的重点在onEnter函数中,值得仔细思考下。


HelloWorldScene.h:

[cpp]  view plain copy
  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5. #include "cocos-ext.h"  
  6.   
  7. USING_NS_CC;  
  8. USING_NS_CC_EXT;  
  9.   
  10. class HelloWorld : public cocos2d::CCLayer  
  11. {  
  12. public:  
  13.     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone  
  14.     virtual bool init();  
  15.       
  16.     // there's no 'id' in cpp, so we recommand to return the exactly class pointer  
  17.     static cocos2d::CCScene* scene();  
  18.       
  19.     // a selector callback  
  20.     void menuCloseCallback(CCObject* pSender);  
  21.       
  22.     // implement the "static node()" method manually  
  23.     CREATE_FUNC(HelloWorld);  
  24.       
  25. public:  
  26.     virtual void onEnter();  
  27.     virtual void registerWithTouchDispatcher();  
  28.       
  29.     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  30.     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);  
  31.     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);  
  32.     virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);  
  33.       
  34. private:  
  35.     // 菜单回调  
  36.     void menu1Callback(CCNode *pSender);  
  37.     void menu2Callback(CCNode *pSender);  
  38.       
  39. private:  
  40.     CCMenu *menu;  
  41.     CCScrollView *scrollView;  
  42. };  
  43.   
  44. #endif // __HELLOWORLD_SCENE_H__  

HelloWorldScene.cpp

[cpp]  view plain copy
  1. #include "HelloWorldScene.h"  
  2. #include "SimpleAudioEngine.h"  
  3.   
  4. using namespace cocos2d;  
  5. using namespace CocosDenshion;  
  6. USING_NS_CC_EXT;  
  7.   
  8. CCScene* HelloWorld::scene()  
  9. {  
  10.     // 'scene' is an autorelease object  
  11.     CCScene *scene = CCScene::create();  
  12.       
  13.     // 'layer' is an autorelease object  
  14.     HelloWorld *layer = HelloWorld::create();  
  15.       
  16.     // add layer as a child to scene  
  17.     scene->addChild(layer);  
  18.       
  19.     // return the scene  
  20.     return scene;  
  21. }  
  22.   
  23. // on "init" you need to initialize your instance  
  24. bool HelloWorld::init()  
  25. {  
  26.     //  
  27.     // 1. super init first  
  28.     if ( !CCLayer::init() )  
  29.     {  
  30.         return false;  
  31.     }  
  32.       
  33.     setTouchEnabled(true);  
  34.       
  35.     /  
  36.     // 3. add your codes below...  
  37.       
  38.     // ask director the window size  
  39.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  40.       
  41.     // CCScrollView  
  42.     scrollView = CCScrollView::create();  
  43.     CCLayer *layer = CCLayer::create();  
  44.       
  45.     CCSprite *sprite1 = CCSprite::create("HelloWorld.png");  
  46.     CCSprite *sprite2 = CCSprite::create("HelloWorld.png");  
  47.       
  48.     layer->setAnchorPoint(ccp(0, 0));  
  49.     layer->setPosition(ccp(0, 0));  
  50.       
  51.     // Menu  
  52.     CCMenuItemSprite *menuItem1 = CCMenuItemSprite::create(sprite1, sprite1, this, menu_selector(HelloWorld::menu1Callback));  
  53.     float scale = 0.5;  
  54.     CCSize itemSize(sprite1->getTextureRect().size);  
  55.     menuItem1->setPosition(ccpAdd(CCPointZero, ccp(itemSize.width*scale / 2, itemSize.height*scale / 2)));  
  56.     menuItem1->setScale(scale);  
  57.     CCMenuItemSprite *menuItem2 = CCMenuItemSprite::create(sprite2, sprite2, this, menu_selector(HelloWorld::menu2Callback));  
  58.     //    menuItem2->setPosition(ccpAdd(ccp(480, 0), ccp(size.width / 2, size.height / 2)));  
  59.     menuItem2->setPosition(ccp(itemSize.width*scale*3/2, itemSize.height*scale / 2));  
  60.     menuItem2->setScale(scale);  
  61.       
  62.     menu = CCMenu::create(menuItem1, menuItem2, NULL);  
  63.     menu->setPosition(CCPointZero);  
  64.       
  65.     layer->addChild(menu);  
  66.     layer->setContentSize(CCSizeMake(itemSize.width*2*scale, 320));  
  67.       
  68.     scrollView->setPosition(ccp(0,0));  
  69.     scrollView->setContentOffset(CCPointZero);  
  70.     scrollView->setContentSize(CCSizeMake(640, 320));  
  71.     scrollView->setContainer(layer);  
  72.     scrollView->setDirection(kCCScrollViewDirectionHorizontal);  
  73.       
  74.     this->addChild(scrollView);  
  75.     return true;  
  76. }  
  77.   
  78. void HelloWorld::onEnter() {  
  79.     CCLayer::onEnter();  
  80.       
  81.     //这里是重点,尝试注释掉它然后再执行程序,看左右拖动图片时行为有什么变化。  
  82.     menu->setHandlerPriority(1);  
  83. }  
  84.   
  85. void HelloWorld::menuCloseCallback(CCObject* pSender)  
  86. {  
  87.     CCDirector::sharedDirector()->end();  
  88.       
  89. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  90.     exit(0);  
  91. #endif  
  92. }  
  93.   
  94. void HelloWorld::registerWithTouchDispatcher()  
  95. {  
  96.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);  
  97. }  
  98.   
  99. bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  100. {  
  101.     return true;  
  102. }  
  103.   
  104. void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  105. {  
  106.     CCLOG("move");  
  107. }  
  108.   
  109. void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  110. {  
  111. }  
  112.   
  113. void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)  
  114. {  
  115. }  
  116.   
  117. void HelloWorld::menu1Callback(cocos2d::CCNode *pSender)  
  118. {  
  119.     CCLOG("menu1Callback");  
  120. }  
  121.   
  122. void HelloWorld::menu2Callback(cocos2d::CCNode *pSender)  
  123. {  
  124.     CCLOG("menu2Callback");  
  125. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值