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

cocos2dx 3.x 屏幕触摸事件的认识,第1张

概述转自:http://blog.csdn.net/lengxue789/article/details/38296029 1. 屏幕触摸事件 EventListenerTouchOneByOne :单点触控 [html]  view plain copy print ? std::function<bool(Touch*, Event*)> onTouchBegan; //触摸开始事件   std:

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


1. 屏幕触摸事件

EventListenertouchOneByOne :单点触控

[HTML] view plain copy print ? 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用于设置是否吞没事件,也就是当某个触摸事件回调的时,截断该事件,让它不能继续传递给其他人。

总结

以上是内存溢出为你收集整理的cocos2dx 3.x 屏幕触摸事件的认识全部内容,希望文章能够帮你解决cocos2dx 3.x 屏幕触摸事件的认识所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1073474.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存