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

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.x 屏幕触摸事件的认识前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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


1. 屏幕触摸事件

EventListenerTouchOneByOne :单点触控

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

copy

void(conststd::vectorTouch*>&,0); font-weight:bold; background-color:inherit">>onTouchesBegan;//触摸开始事件
  • >onTouchesMoved;//触摸移动事件
  • >onTouchesEnded;//触摸结束事件
  • >onTouchesCancelled;//触摸中断事件

  • copy
    boolTouchTest::init()
  • {
  • if(!Layer::init())
  • returnfalse;
  • }
  • //允许接收触摸事件
  • this->setTouchEnabled(true);
  • EventDispatcher*eventDispatcher=Director::getInstance()->getEventDispatcher();
  • autolisten=EventListenerTouchAllAtOnce::create();
  • listen->onTouchesBegan=CC_CALLBACK_2(TouchTest::onTouchesBegan,this);
  • listen->onTouchesMoved=CC_CALLBACK_2(TouchTest::onTouchesMoved,this);
  • >onTouchesEnded=CC_CALLBACK_2(TouchTest::onTouchesEnded,0); font-weight:bold; background-color:inherit">>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">
  • returntrue;
  • //触摸事件开始,手指按下时
  • voidTouchTest::onTouchesBegan(conststd::vector>&touches,cocos2d::Event*event)
  • for(auto&item:touches)
  • autotouch=item;
  • autopos1=touch->getLocation();
  • autopos2=touch->getLocationInView();
  • autopos3=Director::getInstance()->convertToUI(pos2);
  • log("pos1x:%f,y:%f",pos1.x,pos1.y);
  • log("pos2x:%f,pos2.x,pos2.y);
  • log("pos2x:%f,pos3.x,pos3.y);
  • }
  • //触摸移动事件,也就是手指在屏幕滑动的过程
  • voidTouchTest::onTouchesMoved(conststd::vector {
  • log("TouchTestonTouchesMoved");
  • //触摸事件结束,也就是手指松开时
  • voidTouchTest::onTouchesEnded(conststd::vector log("TouchTestonTouchesEnded");
  • //打断触摸事件,一般是系统层级的消息,如来电话,触摸事件就会被打断
  • voidTouchTest::onTouchesCancelled(conststd::vector log("TouchTestonTouchesCancelled");
  • }
  • setTouchEnabled();//设置是否允许接收触摸事件

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

    (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);

    单点触控触摸监听代码

    copy

    this->setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
  • autooneTouch=EventListenerTouchOneByOne::create();
  • 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); font-weight:bold; background-color:inherit">>onTouchEnded=CC_CALLBACK_2(TouchTest::onTouchEnded,0); font-weight:bold; background-color:inherit">>onTouchCancelled=CC_CALLBACK_2(TouchTest::onTouchCancelled,0); font-weight:bold; background-color:inherit">>addEventListenerWithSceneGraphPriority(oneTouch,this);

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

    copy

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

    listen->setSwallowTouches(true);

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

    原文链接:https://www.f2er.com/cocos2dx/339717.html
  • 猜你在找的Cocos2d-x相关文章