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

【Cocos2d-x 3.0学习笔记】触摸

http://blog.csdn.net/lnb333666/article/details/16857637

  1. //触屏事件
  2. virtualboolccTouchBegan(Touch*touch,Event*event);
  3. virtualvoidccTouchMoved(Touch*touch,Event*event);
  4. virtualvoidccTouchEnded(Touch*touch,226); color:inherit; line-height:18px">
  5. //生命周期
  6. virtualvoidonEnter();
  7. virtualvoidonExit();
  8. voidTouchdemo::onEnter()
  9. {
  10. //开启触屏监听
  11. Director::sharedDirector()->getTouchdispatcher()->addTargetedDelegate(this,false);
  12. Layer::onEnter();//一定不要忘了
  13. }
  14. voidTouchdemo::onExit()
  15. {
  16. //关闭触屏监听
  17. Director::sharedDirector()->getTouchdispatcher()->removeDelegate(this);
  18. Layer::onExit();//一定不要忘了
  19. }
  20. ===========================================================================================
  21. 添加委托
  22. addStandardDelegate();
  23. 开启多点触控(不写没有效果哦)
  24. setTouchEnabled();
  25. 触屏事件:
  26. 1.注册多点触控
  27. virtualvoidregisterWithTouchdispatcher(void);
  28. 2.当用户第一次触碰手机屏幕时响应的回调函数
  29. virtualvoidcctouchesBegan(Set*touchs,85); line-height:18px"> 3.当用户手指在手机屏幕上滑动时响应的回调函数
  30. virtualvoidcctouchesMoved(Set*touchs,226); color:inherit; line-height:18px"> 4.当用户手指在离开手机屏幕上时响应的回调函数
  31. virtualvoidcctouchesEnded(Set*touchs,0); background-color:inherit">//写上多点触控的回调函数
  32. virtualvoidcctouchesBegan(Set*set,Event*e);
  33. virtualvoidcctouchesMoved(Set*set,Event*e);
  34. virtualvoidcctouchesEnded(Set*set,0); background-color:inherit">//写上生命周期函数
  35. 注册监听
  36. voidTouchsdemo::registerWithTouchdispatcher(void)
  37. //注册监听
  38. Director::sharedDirector()->getTouchdispatcher()->addStandardDelegate(this,0);
  39. voidTouchsdemo::cctouchesBegan(Set*set,Event*e)
  40. SetIteratoriter=set->begin();
  41. //这里的iter相当于系统分配的数字,
  42. //set->begin()获取一个数字
  43. //set->end()就是最后一位
  44. for(;iter!=set->end();iter++){
  45. //这里呢,就是单点处理了,可以理解为把多点拆成单点来做
  46. CTouch*mytouch=(Touch*)(*iter);
  47. Pointcp=mytouch->getLocation();
  48. //这里我们对5个触摸块进行处理
  49. if(mytouch->getID()==0){
  50. Sprite*sp=(Sprite*)this->getChildByTag(1);
  51. sp->setPosition(cp);
  52. if(mytouch->getID()==1){
  53. Sprite*sp=(Sprite*)this->getChildByTag(2);
  54. if(mytouch->getID()==2){
  55. Sprite*sp=(Sprite*)this->getChildByTag(3);
  56. if(mytouch->getID()==3){
  57. Sprite*sp=(CCSprite*)this->getChildByTag(4);
  58. if(mytouch->getID()==4){
  59. Sprite*sp=(Sprite*)this->getChildByTag(5);
  60. voidTouchsdemo::cctouchesEnded(Set*set,CCEvent*e)
  61. CCSetIteratoriter=set->begin();
  62. inta[6]={0,0};//创建一个数组
  63. Touch*mytouch=(Touch*)(*iter);
  64. Pointcp=mytouch->getLocation();
  65. //这里我们对5个触摸块进行处理
  66. if(mytouch->getID()==0){
  67. a[1]=1;
  68. a[2]=1;
  69. if(mytouch->getID()==2){
  70. a[3]=1;
  71. a[4]=1;
  72. if(mytouch->getID()==4){
  73. a[5]=1;
  74. for(inti=1;i<=5;i++){<>
  75. if(a[i]==1){
  76. Sprite*sp=(Sprite*)this->getChildByTag(i);
  77. sp->runAction(Moveto::create(5,ccp(30,30)));
  78. //实现生命周期函数
  79. voidTouchsdemo::onEnter()
  80. Layer::onEnter();
  81. voidTouchsdemo::onExit()
  82. Layer::onExit();
  83. }

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

相关推荐