加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 3.x 屏幕触摸事件的认识

发布时间:2020-12-14 16:39:47 所属栏目:百科 来源:网络整理
导读:转自:http://blog.csdn.net/lengxue789/article/details/38296029 1. 屏幕触摸事件 EventListenerTouchOneByOne :单点触控 [html] view plain copy print ? std::function bool (Touch*,Event*) onTouchBegan;//触摸开始事件 std::function void (Touch*,0

转自:http://blog.csdn.net/lengxue789/article/details/38296029


1. 屏幕触摸事件

EventListenerTouchOneByOne :单点触控

[html] view plain copy print ?
  1. std::function<bool(Touch*,Event*)>onTouchBegan;//触摸开始事件
  2. std::functionvoid(Touch*,0); background-color:inherit; font-weight:bold">>onTouchMoved;//触摸移动事件
  3. std::function>onTouchEnded;//触摸结束事件
  4. >onTouchCancelled;//触摸中断事件
EventListenerTouchAllAtOnce:多点触控

    void(conststd::vectorTouch*>&,0); background-color:inherit; font-weight:bold">>onTouchesBegan;//触摸开始事件
  1. >onTouchesMoved;//触摸移动事件
  2. >onTouchesEnded;//触摸结束事件
  3. >onTouchesCancelled;//触摸中断事件
下面是多点触摸的例子:
    boolTouchTest::init()
  1. {
  2. if(!Layer::init())
  3. returnfalse;
  4. }
  5. EventDispatcher*eventDispatcher=Director::getInstance()->getEventDispatcher();
  6. autolisten=EventListenerTouchAllAtOnce::create();
  7. listen->onTouchesBegan=CC_CALLBACK_2(TouchTest::onTouchesBegan,this);
  8. listen->onTouchesMoved=CC_CALLBACK_2(TouchTest::onTouchesMoved,this);
  9. >onTouchesEnded=CC_CALLBACK_2(TouchTest::onTouchesEnded,0); background-color:inherit; font-weight:bold">>onTouchesCancelled=CC_CALLBACK_2(TouchTest::onTouchesCancelled,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> eventDispatcher->addEventListenerWithSceneGraphPriority(listen,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">
  10. returntrue;
  11. }
  12. //触摸事件开始,手指按下时
  13. voidTouchTest::onTouchesBegan(conststd::vector>&touches,cocos2d::Event*event)
  14. for(auto&item:touches)
  15. autotouch=item;
  16. autopos1=touch->getLocation();
  17. autopos2=touch->getLocationInView();
  18. autopos3=Director::getInstance()->convertToUI(pos2);
  19. log("pos1x:%f,y:%f",pos1.x,pos1.y);
  20. log("pos2x:%f,pos2.x,pos2.y);
  21. log("pos2x:%f,pos3.x,pos3.y);
  22. }
  23. //触摸移动事件,也就是手指在屏幕滑动的过程
  24. voidTouchTest::onTouchesMoved(conststd::vector {
  25. log("TouchTestonTouchesMoved");
  26. //触摸事件结束,也就是手指松开时
  27. voidTouchTest::onTouchesEnded(conststd::vector log("TouchTestonTouchesEnded");
  28. //打断触摸事件,一般是系统层级的消息,如来电话,触摸事件就会被打断
  29. voidTouchTest::onTouchesCancelled(conststd::vector log("TouchTestonTouchesCancelled");
  30. }

获取单击屏幕时的坐标方式:

(1)touch->getLocation(); 获得单击坐标,基于3D

(2)touch->getLocationInView(); 获取单击坐标,是屏幕坐标系的坐标

(3)Director::getInstance()->convertToUI(pos2); //将屏幕坐标系的坐标,转为cocos2dx的坐标

坐标系:cocos2d-x是基于openGLES的,所以遵循openGL的坐标系,也就是说是以屏幕的左下角为坐标原点。屏幕坐标系一般是以左下角为坐标原点。


2. 修改触控方式

//设置多点触控

this->setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);

//设置单点触控

//this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);

单点触控触摸监听代码:

    this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
  1. autooneTouch=EventListenerTouchOneByOne::create();
  2. oneTouch->onTouchBegan=CC_CALLBACK_2(TouchTest::onTouchBegan,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> oneTouch->onTouchMoved=CC_CALLBACK_2(TouchTest::onTouchMoved,0); background-color:inherit; font-weight:bold">>onTouchEnded=CC_CALLBACK_2(TouchTest::onTouchEnded,0); background-color:inherit; font-weight:bold">>onTouchCancelled=CC_CALLBACK_2(TouchTest::onTouchCancelled,0); background-color:inherit; font-weight:bold">>addEventListenerWithSceneGraphPriority(oneTouch,this);

多点触控的触摸监听代码:

    //设置多点触控
  1. this->setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);
  2. autolisten=EventListenerTouchAllAtOnce::create();
  3. eventDispatcher-3. 屏蔽触摸向下传递

    listen->setSwallowTouches(true);

    setSwallowTouches用于设置是否吞没事件,也就是当某个触摸事件回调的时,截断该事件,让它不能继续传递给其他人。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读