刚刚打完篮球,快中暑了,大脑不转,转载一部分来源:百度
在cocos2dx 3.0版本中,废弃了以往2.x版本的写法,我们先来看一下Layer.h中的一段代码
-
- //单点触摸
- virtual bool onTouchBegan(Touch *touch, Event *unused_event);
- virtual void onTouchMoved(Touch *touch, Event *unused_event);
- virtual void onTouchEnded(Touch *touch, Event *unused_event);
- virtual void onTouchCancelled(Touch *touch, Event *unused_event);
- //多点触摸
- virtual void onTouchesBegan(const std::vector<Touch*>& touches, Event *unused_event);
- virtual void onTouchesMoved(const std::vector<Touch*>& touches, Event *unused_event);
- virtual void onTouchesEnded(const std::vector<Touch*>& touches, Event *unused_event);
- virtual void onTouchesCancelled(const std::vector<Touch*>&touches, Event *unused_event);
单点触摸:(即只有注册的Layer才能接收触摸事件)
onTouchBegan: 如果返回true:本层的后续Touch事件可以被触发,并阻挡向后层传递
如果返回false,本层的后续Touch事件不能被触发,并向后传递,也就是不会调用onTouchMoved
简单点来说,如果
1.Layer 只有一层的情况:
单点触摸简单用法:
在Layer中添加如下代码,重写onTouchxxx函数
- auto dispatcher = Director::getInstance()->getEventDispatcher();
- auto listener = 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,this);
- listener->setSwallowTouches(true);//不向下传递触摸
- dispatcher->addEventListenerWithSceneGraphPriority(listener,this);
listener->setSwallowTouches(true),不向下触摸,简单点来说,比如有两个sprite ,A 和 B,A在上B在下(位置重叠),触摸A的时候,B不会受到影响
listener->setSwallowTouches(false)反之,向下传递触摸,触摸A也等于触摸了B多点触摸点单用法(多个Layer获取屏幕事件):
- auto dispatcher = Director::getInstance()->getEventDispatcher();
- auto listener1 = EventListenerTouchAllAtOnce::create();
- listener1->onTouchesBegan = CC_CALLBACK_2(GameLayer::onTouchesBegan,this);
- listener1->onTouchesMoved = CC_CALLBACK_2(GameLayer::onTouchesMoved,this);
- listener1->onTouchesEnded = CC_CALLBACK_2(GameLayer::onTouchesEnded,this);
- dispatcher->addEventListenerWithSceneGraphPriority(listener1,this);
关于eventDispatcher:
-
获取方法:
-
- auto dispatcher = Director::getInstance()->getEventDispatcher();
事件监听器包含以下几种:
- 触摸事件 (EventListenerTouch)
- 键盘响应事件 (EventListenerKeyboard)
- 加速记录事件 (EventListenerAcceleration)
- 鼠标响应事件 (EventListenerMouse)
- 自定义事件 (EventListenerCustom)
以上事件监听器统一由
_eventDispatcher
来进行管理。
- void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
- void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
代码展开一下:
- void EventDispatcher::addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node)
- {
- CCASSERT(listener && node, "Invalid parameters.");
- CCASSERT(!listener->isRegistered(), "The listener has been registered.");
- if (!listener->checkAvailable())
- return;
- listener->setSceneGraphPriority(node);
- listener->setFixedPriority(0);
- listener->setRegistered(true);
- addEventListener(listener);
- }
- void EventDispatcher::addEventListenerWithFixedPriority(EventListener* listener, int fixedPriority)
- {
- CCASSERT(listener, "Invalid parameters.");
- CCASSERT(!listener->isRegistered(), "The listener has been registered.");
- CCASSERT(fixedPriority != 0, "0 priority is forbidden for fixed priority since it's used for scene graph based priority.");
- if (!listener->checkAvailable())
- return;
- listener->setSceneGraphPriority(nullptr);
- listener->setFixedPriority(fixedPriority);
- listener->setRegistered(true);
- listener->setPaused(false);
- addEventListener(listener);
- }
- 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);