转自:http://blog.csdn.net/lengxue789/article/details/38296029
1. 屏幕触摸事件
EventListenerTouchOneByOne :单点触控
- std::function<bool(Touch*,Event*)>onTouchBegan;//触摸开始事件
- std::functionvoid(Touch*,0); background-color:inherit; font-weight:bold">>onTouchMoved;//触摸移动事件
- std::function>onTouchEnded;//触摸结束事件
- >onTouchCancelled;//触摸中断事件
EventListenerTouchAllAtOnce:多点触控
void(conststd::vectorTouch*>&,0); background-color:inherit; font-weight:bold">>onTouchesBegan;//触摸开始事件
- >onTouchesMoved;//触摸移动事件
- >onTouchesEnded;//触摸结束事件
- >onTouchesCancelled;//触摸中断事件
下面是多点触摸的例子:
boolTouchTest::init()
- {
- if(!Layer::init())
- returnfalse;
- }
- 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); 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">
- 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");
- }
获取单击屏幕时的坐标方式:
(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);
- 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); 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);
多点触控的触摸监听代码:
//设置多点触控
- this->setTouchMode(Touch::DispatchMode::ALL_AT_ONCE);
- autolisten=EventListenerTouchAllAtOnce::create();
- eventDispatcher-3. 屏蔽触摸向下传递
listen->setSwallowTouches(true);
setSwallowTouches用于设置是否吞没事件,也就是当某个触摸事件回调的时,截断该事件,让它不能继续传递给其他人。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|