Cocos2d-x 详细事件分发机制推荐看http://www.cnblogs.com/haogj/p/3784896.html
简单区分几种EventListener和_eventDispatcher添加监听时的区别。
监听器分为:
EventListenerTouch 触摸监听
EventListenerKeyboard 键盘监听
EventListenerMouse 鼠标监听
EventListenerCustom 自定义监听
EventListenerAcceleration 加速计监听
前三种是系统自带的事件类型(由系统触发) 后两种是用户设置的事件类型(人为触发)
一个操作可以触发多个事件
_eventDispatcher->addEventListenerWithSceneGraphPriority(cocos2d::EventListener *listener, cocos2d::Node *node);
_eventDispatcher->addEventListenerWithFixedPriority(cocos2d::EventListener *listener, int fixedPriority);
第一种更常用,相当于监听器绑定到节点上,在上层(Z Order大的)的优先响应,方便设计时多层节点层叠时的事件监听。
第二种,相当于给监听器一个优先级,数字越小越优先响应。
以下两种没有用过~~~~~~~~~~~~~~~
_eventDispatcher->addCustomEventListener(const std::string &eventName, const std::function &callback);
自定义事件监听器怎么用?????
在后面
_eventDispatcher->addEventListener(cocos2d::EventListener *listener);
为什么不提倡这个??????
EventListenerTouch 触摸监听 包括单点和多点
单点触摸事件中的onTouchCancelled事件在设备中断操作(有新通知或者电话出现时)时回触发,一般可以讲onTouchEnded中的操作也写到该函数中
//单点触摸监听,精灵跟随鼠标移动,释放后回到中心
//onTouchCancelled该怎样触发??????????????触点划出屏幕时不会触发这个!!!!!!!!!!!!!!!!!手机通知或者电话导致中断会触发
auto listenerTouch=EventListenerTouchOneByOne::create();
//在Began中返回true可以使触摸不传递到下层,及吞掉触摸事件
listenerTouch->onTouchBegan=[=](Touch* touch,Event* event){
boy->setPosition(touch->getLocation());
CCLOG("Touch Began!");
return true;
};
listenerTouch->onTouchMoved=[=](Touch* touch,Event* event){
boy->setPosition(touch->getLocation());
CCLOG("Touch Moved!");
};
listenerTouch->onTouchEnded=[=](Touch* touch,Event* event){
boy->runAction(MoveTo::create(0.5f, Vec2(480,320)));
CCLOG("Touch Ended!");
};
listenerTouch->onTouchCancelled=[=](Touch* touch,Event* event){
CCLOG("Touch Cancelled!");
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch, this);
//多点触摸暂时没有调试~~~~~~~~~~~~~~~~~~~
EventListenerMouse 鼠标监听
鼠标监听中事件包括onMouseMove onMouseUp onMouseDown onMouseScroll
//鼠标事件
auto listenerMouse=EventListenerMouse::create();
listenerMouse->onMouseUp=[](Event* event){
EventMouse* e=(EventMouse*)event;//转换为EventMouse才能读取
log("%d",e->getMouseButton());
};
listenerMouse->onMouseScroll=[](Event* event){
EventMouse* e=(EventMouse*)event;
log("scroll X:%f Y:%f",e->getScrollX(),e->getScrollY());
//X轴方向的滚动是什么鬼?
};
listenerMouse->onMouseMove=[](Event* event){
EventMouse* e=(EventMouse*)event;
//log("%d",e->getMouseButton());
log("X:%f/tY:%f",e->getCursorX(),e->getCursorY());//根据笛卡尔坐标显示,不是cocos坐标
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerMouse, this);
EventListenerKeyboard 键盘监听事件
键盘监听事件只包括onKeyPressed onKeyReleased
//键盘按键监听
auto listenerKey=EventListenerKeyboard::create();
listenerKey->onKeyPressed=[=](EventKeyboard::KeyCode keyCode,Event* event){
if (keyCode==EventKeyboard::KeyCode::KEY_A) {
log("AAAAA");//可以直接用==
}
else log("Pressed Key:%d",keyCode);
};
listenerKey->onKeyReleased=[=](EventKeyboard::KeyCode keyCode,Event* event){
log("Released Key:%d",keyCode);
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerKey, this);
EventListenerCustom 自定义监听器
自定义监听器需要通过对应的事件来触发,定义事件时要与监听器名字对应
//自定义监听器,需要定义触发条件,需要手动触发,手动触发时注意自定义监听器的名字
auto listenerCustom=EventListenerCustom::create("Event Name 1", [=](EventCustom* event){
log("Custom Event,User Data:%s",event->getUserData());
});
_eventDispatcher->addEventListenerWithFixedPriority(listenerCustom, 1);
//定义一个触摸监听,在触摸时触发自定义监听
auto listenerTouch=EventListenerTouchOneByOne::create();
listenerTouch->onTouchBegan=[=](Touch* touch,Event* event){
log("Touch Listener1!");
//触发自定义监听器
EventCustom myEvent("Event Name 1");
char data[20]="I am data of user!";
myEvent.setUserData(data);
_eventDispatcher->dispatchEvent(&myEvent);//不是dispatchCustomEvent
log("Touch Listener2!");//测试事件被自定义监听器处理后,触摸事件的监听器能否继续处理,测试结果是可以的
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listenerTouch, this);
EventListenerAcceleration 加速计事件
加速计事件还没有测试~~~~~~~~~~~~~~~~