当前位置: 首页 > article >正文

CCTouch触摸事件

一、CCTouch触摸事件

cocos2d-x触摸事件分为两种,单点触摸和多点触摸,单点触摸是我们最常用的触摸方式。要使用单点触摸必须要实现一下几个函数

virtual void registerWithTouchDispatcher(void);
    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);//必须实现
    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);

不用必须实现CCLayer的onEnter()和onExit()方法,在registerWithTouchDispatcher中会自动完成触摸事件的注册和移除,要开启触摸事件,在初始化时必须设置setTouchEnabled(true),默认开启的是多点触摸,在registerWithTouchDispatcher方法中需要我们添加单点触摸。例子中在testLayer中添加了一个按钮和一个标签,当点击按钮的时候会弹出一个半透明的viewLayer遮盖在testLayer上面,viewLayer上面添加有一个标签。我们可以通过改变按钮和各层的优先级来看见不同的显示效果,详细运行效果可见工程源码。

代码如下:
  1. bool TouchTest::init()
  2. {
  3.     if(!CCLayer::init())
  4.     {
  5.         return false;
  6.     }
  7.     setTouchEnabled(true);
  8.     size=CCDirector::sharedDirector()->getWinSize();
  9.     CCSprite* background=CCSprite::create("background.jpg");
  10.     background->setPosition(ccp(size.width/2, size.height/2));
  11.     this->addChild(background);
  12.     
  13.     CCLabelTTF* pLabel=CCLabelTTF::create("This is testLayer", "", 35);
  14.     pLabel->setAnchorPoint(ccp(0.5,0.5));
  15.     pLabel->setColor(ccc3(30, 40, 50));
  16.     pLabel->setPosition(ccp(size.width/2,size.height/2-100));
  17.     this->addChild(pLabel);
  18.     
  19.     CCMenuItemImage* item=CCMenuItemImage::create("confirmA.png", "confirmB.png", this, menu_selector(TouchTest::openLayer));
  20.     item->setPosition(ccp(size.width/2,size.height/2));
  21.     CCMenu* menu=CCMenu::create(item,NULL);
  22.     menu->setPosition(CCPointZero);
  23.     menu->setTouchPriority(-1);//CCmenu默认的优先级是-128
  24.     this->addChild(menu);
  25.     
  26.     return true;
  27. }

  28. void TouchTest::openLayer()
  29. {
  30.     ViewLayer* layer=ViewLayer::create();
  31.     this->addChild(layer);
  32. }

  33. void TouchTest::registerWithTouchDispatcher(void)
  34. {/*注册单点触摸事件,可以改变第二参数的数值和CCMenu对比,第三个参数的意思是是否吞掉后续触摸事件,当设置为true时,当该CCTouchDelegate所在层的ccTouchBegan返回为true时,触摸消息响应之后就会被吞掉,反之则能继续传递下去,通常都设置为true*/
  35.     CCLayer::registerWithTouchDispatcher();
  36.     CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
  37. }

  38. bool TouchTest::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
  39. {
  40.     CCLog("Touch testlayer");
  41.     return true;//此处必须返回true,否者不会响应后面的事件。
  42. }

  43. void TouchTest::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
  44. {
  45.     
  46. }

  47. void TouchTest::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
  48. {
  49.     
  50. }

  51. void TouchTest::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
  52. {
  53.     
  54. }
复制代码
http://www.lryc.cn/news/2416182.html

相关文章:

  • java递归函数的解析
  • 全国大学生统计建模大赛历年获奖论文集下载教程(知网)
  • adobe dreamweaver cs5序列号
  • 安卓开发学习笔记(1) preferences实现设置界面
  • Hook api! 如何拦截系统api, 让它做你想做的事!
  • 程序框图与计算机程序,_算法与程序框图_ppt.ppt
  • MyEclipse-7.5.0版注册码破解及激活操作
  • 最全音频总线汇总
  • Web 项目中分享到微博、QQ空间等分享功能
  • CheckBoxList、DropDownList、ListBox、RadioButtonList、BulletedList对于符号的解释区别
  • BeyondCompare3密钥过期怎么办?不用再找新的密钥,一招帮你搞定!
  • dell 笔记本禁用触摸板方法
  • 【游戏逆向】CS1.6无限手雷辅助
  • 红客联盟是什么?红客需要传承!
  • 用WordPress开发app之拿去即用的Blog-App
  • 屏幕录像专家6.0_电脑屏幕录像直播软件推荐?功能全面的专业软件分享
  • WSockExpert[抓包工具]
  • 开源软件抄袭 美国_本网站共享来自美国政府多个项目的开源代码
  • 404是什么意思,404错误页面有什么用?
  • Freehostia 老牌稳定免费空间申请教学
  • Ecall测试,ITU-T P.1140 车载紧急呼叫系统语音测试
  • python-正则表达式及应用(b站弹幕屏蔽)
  • 我的ESP32学习(5)AP和后续长远布局
  • Squirrel SQL Client客户端工具
  • 轻松应对:《精灵与萤火意志》游戏缺失iutils.dll文件的终极解决指南
  • 搜索引擎分类与工作原理
  • Android 根目录和sdcard卡路径分析
  • NR 5G 密钥与安全详解
  • 人品计算器
  • Conficker蠕虫病毒专杀工具集锦