说明
事件分发机制
该机制包含三部分内容:事件分发器、事件类型、事件监听器;
EventDispatcher@H_502_12@
EventListener@H_502_12@
监听器类型:
enumclass Type { UNKNOWN, TOUCH_ONE_BY_ONE TOUCH_ALL_AT_ONCE KEYBOARD MOUSE ACCELERATION FOCUS GAME_CONTROLLER CUSTOM};
监听器类型较多,具体每一个类型都继承于EventListener,下面介绍几个常用的监听器用法:
- EventListenerTouchOneByOne
// 创建一个事件监听器类型为OneByOne的单点触摸auto listener1 = EventListenerTouchOneByOne::create();// 设置是否吞没事件,在onTouchBegan方法返回true时吞没 listener1->setSwallowTouches(true); //注册事件回调onTouchBegan CC_CALLBACK_2(Test2onTouchBeganthisonTouchMoved onTouchMovedonTouchEnded onTouchEnded// 添加监听器 _eventDispatcheraddEventListenerWithSceneGraPHPriority(listener1 pLayer1);
- EventListenerTouchAllAtOnce
- EventListenerMouse
- EventListenerKeyBoard
- EventListenerAcceleration
- EventListenerCustom
Event@H_502_12@
事件类型:
TOUCH};
监听器类型:
enumclass Type { UNKNOWN, TOUCH_ONE_BY_ONE TOUCH_ALL_AT_ONCE KEYBOARD MOUSE ACCELERATION FOCUS GAME_CONTROLLER CUSTOM};
监听器类型较多,具体每一个类型都继承于EventListener,下面介绍几个常用的监听器用法:
- EventListenerTouchOneByOne
// 创建一个事件监听器类型为OneByOne的单点触摸auto listener1 = EventListenerTouchOneByOne::create();// 设置是否吞没事件,在onTouchBegan方法返回true时吞没 listener1->setSwallowTouches(true); //注册事件回调onTouchBegan CC_CALLBACK_2(Test2onTouchBeganthisonTouchMoved onTouchMovedonTouchEnded onTouchEnded// 添加监听器 _eventDispatcheraddEventListenerWithSceneGraPHPriority(listener1 pLayer1);
- EventListenerTouchAllAtOnce
- EventListenerMouse
- EventListenerKeyBoard
- EventListenerAcceleration
- EventListenerCustom
// 创建一个事件监听器类型为OneByOne的单点触摸auto listener1 = EventListenerTouchOneByOne::create();// 设置是否吞没事件,在onTouchBegan方法返回true时吞没 listener1->setSwallowTouches(true); //注册事件回调onTouchBegan CC_CALLBACK_2(Test2onTouchBeganthisonTouchMoved onTouchMovedonTouchEnded onTouchEnded// 添加监听器 _eventDispatcheraddEventListenerWithSceneGraPHPriority(listener1 pLayer1);