Cocos2d-x 3.0-触摸机制

Cocos2d-x 3.0-触摸机制,第1张

概述在cocos2dx 3.0版本中,废弃了以往2.x版本的写法,我们先来看一下Layer.h中的一段代码 [cpp]  view plain copy     //单点触摸       virtual bool onTouchBegan(Touch *touch, Event *unused_event);        virtual void onTouchMoved(Touch *touch,

在cocos2dx 3.0版本中,废弃了以往2.x版本的写法,我们先来看一下Layer.h中的一段代码

[cpp] view plain copy
//单点触摸 virtualboolontouchBegan(touch*touch,Event*unused_event); virtualvoIDontouchmoved(touch*touch,Event*unused_event); virtualvoIDontouchended(touch*touch,248)"> virtualvoIDontouchCancelled(touch*touch,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> //多点触摸 virtualvoIDontouchesBegan(conststd::vector<touch*>&touches,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> virtualvoIDontouchesMoved(conststd::vector<touch*>&touches,248)"> virtualvoIDontouchesEnded(conststd::vector<touch*>&touches,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> virtualvoIDontouchesCancelled(conststd::vector<touch*>&touches,Event*unused_event);

单点触摸:(即只有注册的Layer才能接收触摸事件)

ontouchBegan: 如果返回true:本层的后续touch事件可以被触发,并阻挡向后层传递

如果返回false,本层的后续touch事件不能被触发,并向后传递,也就是不会调用ontouchmoved

简单点来说,如果

1.Layer 只有一层的情况:

virtualboolontouchBegan(CCtouch*ptouch,CCEvent*pEvent); a.返回false,则cctouchmoved(),cctouchended()不会再接收到消息 b.返回true,则cctouchmoved(),cctouchended()可以接收到消息 2.Layer 有多层的情况: CCEvent*pEvent);
a.返回false,则本层的ontouchmoved(),ontouchended()不会再接收到消息,但是本层之下的其它层会接收到消息 b.返回true,则本层的ontouchmoved(),ontouchended()可以接收到消息,但是本层之下的其它层不能再接收到消息

单点触摸简单用法:

在Layer中添加如下代码,重写ontouchxxx函数

autodispatcher=Director::getInstance()->getEventdispatcher(); autoListener=EventListenertouchOneByOne::create(); Listener->ontouchBegan=CC_CALLBACK_2(GameLayer::ontouchBegan,this); Listener->ontouchmoved=CC_CALLBACK_2(GameLayer::ontouchmoved,this); Listener->ontouchended=CC_CALLBACK_2(GameLayer::ontouchended,248)"> Listener->setSwallowtouches(true);//不向下传递触摸 dispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);

Listener->setSwallowtouches(true),不向下触摸,简单点来说,比如有两个sprite,A 和 B,A在上B在下(位置重叠),触摸A的时候,B不会受到影响

Listener->setSwallowtouches(false)反之,向下传递触摸,触摸A也等于触摸了B

多点触摸点单用法(多个Layer获取屏幕事件):

autoListener1=EventListenertouchAllAtOnce::create(); Listener1->ontouchesBegan=CC_CALLBACK_2(GameLayer::ontouchesBegan,248)"> Listener1->ontouchesMoved=CC_CALLBACK_2(GameLayer::ontouchesMoved,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> Listener1->ontouchesEnded=CC_CALLBACK_2(GameLayer::ontouchesEnded,248)"> dispatcher->addEventListenerWithSceneGraPHPriority(Listener1,this); 或者settouchEnabled(true),然后重写layer的ontouchsxxx函数

关于eventdispatcher:

获取方法:

[cpp] view plain copy autodispatcher=Director::getInstance()->getEventdispatcher();

事件监听器包含以下几种:

触摸事件 (EventListenertouch) 键盘响应事件 (EventListenerKeyboard) 加速记录事件 (EventListeneracceleration) 鼠标响应事件 (EventListenerMouse) 自定义事件 (EventListenerCustom)

以上事件监听器统一由_eventdispatcher来进行管理。

优先权: 1.优先级越低,越先响应事件 2.如果优先级相同,则上层的(z轴)先接收触摸事件
有两种方式将 事件监听器 Listener1 添加到 事件调度器_eventdispatcher 中:

[cpp] view plain copy voIDEventdispatcher::addEventListenerWithSceneGraPHPriority(EventListener*Listener,Node*node) voIDEventdispatcher::addEventListenerWithFixedPriority(EventListener*Listener,intfixedPriority)
代码展开一下: { CCASSERT(Listener&&node,"InvalIDparameters."); CCASSERT(!Listener->isRegistered(),"TheListenerhasbeenregistered."); if(!Listener->checkAvailable()) return; Listener->setSceneGraPHPriority(node); Listener->setFixedPriority(0); Listener->setRegistered(true); addEventListener(Listener); }
voIDEventdispatcher::addEventListenerWithFixedPriority(EventListener*Listener,intfixedPriority) CCASSERT(Listener,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> CCASSERT(fixedPriority!=0,"0priorityisforbIDdenforfixedprioritysinceit'susedforscenegraphbasedpriority."); if(!Listener->checkAvailable()) return; Listener->setSceneGraPHPriority(nullptr); Listener->setFixedPriority(fixedPriority); Listener->setRegistered(true); Listener->setPaused(false); }

(1)addEventListenerWithSceneGraPHPriority 的事件监听器优先级是0,而且在 addEventListenerWithFixedPriority 中的事件监听器的优先级不可以设置为 0,因为这个是保留给 SceneGraPHPriority 使用的。
(2)另外,有一点非常重要,FixedPriority Listener添加完之后需要手动remove,而SceneGraPHPriority Listener是跟node绑定的,在node的析构函数中会被移除。
移除方法: dispatcher->removeEventListener(Listener);

其实还可以这么用:

//1加入用户触摸事件侦听

auto Listener=EventListenertouchOneByOne::create();

Listener->setSwallowtouches(true);

Listener->ontouchBegan=[&](touch * t,Event * e){

//改变贪食蛇移动的方向

int col=t->getLocation().x/32;

int row=t->getLocation().y/32;

int spheadCol=sphead->getpositionX()/32;

int spheadRow=sphead->getpositionY()/32;

if(abs(spheadCol-col)>abs(spheadRow-row))

{

if(spheadCol<col)

{

sphead->m_dir=ENUM_DIR::DIR_RIGHT;

}else

{

sphead->m_dir=ENUM_DIR::DIR_left;

}

}

else

{if(spheadRow<row)

{

sphead->m_dir=ENUM_DIR::DIR_UP;

}else

{

sphead->m_dir=ENUM_DIR::DIR_DOWN;

}

}

return true;

}; //这条语句像不像Java中的匿名接口对象,像不像Objective-C中Block ,他有个很华丽的名字 lambda,读音为:腊母达,相信未来你会喜欢上这妹子的

//3注册这个侦听到消息分发器中

_eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,this);

总结

以上是内存溢出为你收集整理的Cocos2d-x 3.0-触摸机制全部内容,希望文章能够帮你解决Cocos2d-x 3.0-触摸机制所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存