1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
//
class
EventDispatcher :
public
Ref
{
/**
* 添加监听器
* - addEventListenerWithSceneGraphPriority
* - addEventListenerWithFixedPriority
* - addCustomEventListener
*/
//使用 场景图的优先级 为指定事件添加一个监听.
//listener : 指定要监听的事件.
//node : 这个节点的绘制顺序是基于监听优先级.
//优先级 : 0
void
addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
//使用 一定的优先级 为指定事件添加一个监听.
//listener : 指定要监听的事件.
//fixedPriority : 这个监听器的固定优先级.
//优先级 : fixedPriority。(但是不能为0,因为他是场景图的基本优先级)
void
addEventListenerWithFixedPriority(EventListener* listener,
int
fixedPriority);
//用户自定义监听器
EventListenerCustom* addCustomEventListener(
const
std::string &eventName,
const
std::function<
void
(EventCustom*)>& callback);
/**
* 删除监听器
* - removeEventListener
* - removeEventListenersForType
* - removeEventListenersForTarget
* - removeCustomEventListeners
* - removeAllEventListeners
*/
//删除指定监听器
void
removeEventListener(EventListener* listener);
//删除某类型对应的所有监听器
//EventListener::Type::
// 单点触摸 : TOUCH_ONE_BY_ONE
// 多点触摸 : TOUCH_ALL_AT_ONCE
// 键盘 : KEYBOARD
// 鼠标 : MOUSE
// 加速计 : ACCELERATION
// 自定义 : CUSTOM
void
removeEventListenersForType(EventListener::Type listenerType);
//删除绑定在节点target上的所有监听器
void
removeEventListenersForTarget(Node* target,
bool
recursive =
false
);
//删除名字为customEventName的所有自定义监听器
void
removeCustomEventListeners(
const
std::string& customEventName);
//移除所有监听器
void
removeAllEventListeners();
/**
* 暂停、恢复在节点target上的所有监听器
* - pauseEventListenersForTarget
* - resumeEventListenersForTarget
*/
void
pauseEventListenersForTarget(Node* target,
bool
recursive =
false
);
void
resumeEventListenersForTarget(Node* target,
bool
recursive =
false
);
/**
* 其他
* - setPriority
* - setEnabled
* - dispatchEvent
* - dispatchCustomEvent
*/
//设置某监听器的优先级
void
setPriority(EventListener* listener,
int
fixedPriority);
//启用事件分发器
void
setEnabled(
bool
isEnabled);
bool
isEnabled()
const
;
//手动派发自定义事件
void
dispatchEvent(Event* event);
//给名字为eventName的自定义监听器, 绑定用户数据
void
dispatchCustomEvent(
const
std::string &eventName,
void
*optionalUserData = nullptr);
}
//
|
1
2
3
4
5
6
7
8
|
//
static
EventListenerTouchOneByOne* create();
std::function<
bool
(Touch*, Event*)> onTouchBegan;
//只有这个返回值为 bool
std::function<
void
(Touch*, Event*)> onTouchMoved;
std::function<
void
(Touch*, Event*)> onTouchEnded;
std::function<
void
(Touch*, Event*)> onTouchCancelled;
//
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//
//获取事件分发器
auto
dispatcher = Director::getInstance()->getEventDispatcher();
//创建单点触摸监听器 EventListenerTouchOneByOne
auto
touchListener = EventListenerTouchOneByOne::create();
//单点触摸响应事件绑定
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan,
this
);
touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved,
this
);
touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded,
this
);
touchListener->onTouchCancelled = CC_CALLBACK_2(HelloWorld::onTouchCancelled,
this
);
//在事件分发器中,添加触摸监听器,事件响应委托给 this 处理
dispatcher->addEventListenerWithSceneGraphPriority(touchListener,
this
);
//单点触摸事件响应函数
bool
onTouchBegan(Touch *touch, Event *unused_event) { CCLOG(
"began"
);
return
true
; }
void
onTouchMoved(Touch *touch, Event *unused_event) { CCLOG(
"moved"
); }
void
onTouchEnded(Touch *touch, Event *unused_event) { CCLOG(
"ended"
); }
void
onTouchCancelled(Touch *touch, Event *unused_event) { CCLOG(
"cancelled"
); }
//
|
1
2
3
4
5
6
7
8
|
//
static
EventListenerTouchAllAtOnce* create();
std::function<
void
(
const
std::vector
std::function<
void
(
const
std::vector
std::function<
void
(
const
std::vector
std::function<
void
(
const
std::vector
//
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//
//获取事件分发器
auto
dispatcher = Director::getInstance()->getEventDispatcher();
//创建多点触摸监听器 EventListenerTouchAllAtOnce
auto
touchesListener = EventListenerTouchAllAtOnce::create();
//多点触摸响应事件绑定
touchesListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan,
this
);
touchesListener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved,
this
);
touchesListener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded,
this
);
touchesListener->onTouchesCancelled = CC_CALLBACK_2(HelloWorld::onTouchesCancelled,
this
);
//在事件分发器中,添加触摸监听器,事件响应委托给 this 处理
dispatcher->addEventListenerWithSceneGraphPriority(touchesListener,
this
);
//多点触摸事件响应函数
void
onTouchesBegan(
const
std::vector
"began"
); }
void
onTouchesMoved(
const
std::vector
"moved"
); }
void
onTouchesEnded(
const
std::vector
"ended"
); }
void
onTouchesCancelled(
const
std::vector
"cancelled"
); }
//
|
1
2
3
4
5
6
7
8
|
//
static
EventListenerMouse* create();
std::function<
void
(Event* event)> onMouseDown;
//按下鼠标, 单击鼠标
std::function<
void
(Event* event)> onMouseUp;
//松开鼠标, 按下的状态下松开
std::function<
void
(Event* event)> onMouseMove;
//移动鼠标, 在屏幕中移动
std::function<
void
(Event* event)> onMouseScroll;
//滚动鼠标, 滚动鼠标的滚轮
//
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//
//获取事件分发器
auto
dispatcher = Director::getInstance()->getEventDispatcher();
//创建鼠标事件监听器 EventListenerMouse
EventListenerMouse* mouseListenter = EventListenerMouse::create();
//鼠标事件响应函数
mouseListenter->onMouseDown = CC_CALLBACK_1(HelloWorld::onMouseDown,
this
);
mouseListenter->onMouseUp = CC_CALLBACK_1(HelloWorld::onMouseUp,
this
);
mouseListenter->onMouseMove = CC_CALLBACK_1(HelloWorld::onMouseMove,
this
);
mouseListenter->onMouseScroll = CC_CALLBACK_1(HelloWorld::onMouseScroll,
this
);
//添加鼠标事件监听器,事件响应处理委托给this
dispatcher->addEventListenerWithSceneGraphPriority(mouseListenter,
this
);
//事件响应函数
void
onMouseDown(Event* event) { CCLOG(
"Down"
); }
void
onMouseUp(Event* event) { CCLOG(
"UP"
); }
void
onMouseMove(Event* event) { CCLOG(
"MOVE"
); }
void
onMouseScroll(Event* event) { CCLOG(
"Scroll"
); }
//
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//
static
EventListenerKeyboard* create();
std::function<
void
(EventKeyboard::KeyCode, Event*)> onKeyPressed;
//按下某键
std::function<
void
(EventKeyboard::KeyCode, Event*)> onKeyReleased;
//松开某键
//键盘按键枚举类型 EventKeyboard::KeyCode
//KeyCode的值对应的不是键盘的键值、也不是ASCII码,只是纯粹的枚举类型
//如:
// EventKeyboard::KeyCode::KEY_A
// EventKeyboard::KeyCode::KEY_1
// EventKeyboard::KeyCode::KEY_F1
// EventKeyboard::KeyCode::KEY_SPACE
// EventKeyboard::KeyCode::KEY_ALT
// EventKeyboard::KeyCode::KEY_SHIFT
//
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//
//获取事件分发器
auto
dispatcher = Director::getInstance()->getEventDispatcher();
//创建键盘按键事件监听器
EventListenerKeyboard* keyboardListener = EventListenerKeyboard::create();
//绑定事件响应函数
keyboardListener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed,
this
);
keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased,
this
);
//添加监听器
dispatcher->addEventListenerWithSceneGraphPriority(keyboardListener,
this
);
//事件响应函数
void
onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event) {
if
(EventKeyboard::KeyCode::KEY_J == keyCode) {
CCLOG(
"Pressed: J"
);
}
}
void
onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
if
(EventKeyboard::KeyCode::KEY_SPACE == keyCode) {
CCLOG(
"Released: SPACE"
);
}
}
//
|
1
2
3
4
5
6
|
//加速计信息
class
Acceleration
{
double
x;
double
y;
double
z;
};
//
|
1
2
3
4
5
|
//
static
EventListenerAcceleration* create(
const
std::function<
void
(Acceleration*, Event*)>& callback);
std::function<
void
(Acceleration*, Event*)> onAccelerationEvent;
//
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
//
//标签: 显示加速计信息
label = Label::createWithTTF(
"no used"
,
"Marker Felt.ttf"
, 12);
label->setPosition(visibleSize / 2);
this
->addChild(label);
//小球: 可视化加速计
ball = Sprite::create(
"ball.png"
);
ball->setPosition(visibleSize / 2);
this
->addChild(ball);
//获取事件分发器
auto
dispatcher = Director::getInstance()->getEventDispatcher();
//需要开启移动设备的加速计
Device::setAccelerometerEnabled(
true
);
//创建加速计事件监听器
auto
accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(HelloWorld::onAccelerationEvent,
this
));
//添加加速计监听器
dispatcher->addEventListenerWithSceneGraphPriority(accelerationListener,
this
);
//事件响应函数
void
HelloWorld::onAccelerationEvent(Acceleration* acceleration, Event* event)
{
char
s[100];
sprintf
(s,
"X: %f; Y: %f; Z:%f; "
, acceleration->x, acceleration->y, acceleration->z);
label->setString(s);
//改变小球ball的位置
float
x = ball->getPositionX() + acceleration->x * 10;
float
y = ball->getPositionY() + acceleration->y * 10;
Vec2 pos = Vec2(x, y);
pos.clamp(ball->getContentSize() / 2, Vec2(288, 512) - ball->getContentSize() / 2);
ball->setPosition(pos);
//设置位置
}
//
|
1
2
3
4
5
|
//
//eventName : 监听器名字,即消息的名称
//callback : 监听器函数,即消息的回调函数
static
EventListenerCustom* create(
const
std::string& eventName,
const
std::function<
void
(EventCustom*)>& callback);
//
|
1
2
3
4
5
|
//
EventCustom event(
"custom_event"
);
event->setUserData((
void
*)123);
// 绑定消息传递的数据,可以为任意类型void。
dispatcher->dispatchEvent(&event);
// 发布名称为"custom_event"的消息。
//
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//
//获取事件分发器
auto
dispatcher = Director::getInstance()->getEventDispatcher();
//创建自定义事件监听器
//监听器名字 : "custom_event"
//事件响应函数: HelloWorld::onCustomEvent
auto
customListener = EventListenerCustom::create(
"custom_event"
, CC_CALLBACK_1(HelloWorld::onCustomEvent,
this
));
//添加自定义事件监听器,优先权为1
dispatcher->addEventListenerWithFixedPriority(customListener, 1);
//手动分发监听器的事件,通过dispatchEvent发布名称为custom_event的消息。
EventCustom event = EventCustom(
"custom_event"
);
event->setUserData((
void
*)123);
// 绑定消息传递的数据,可以为任意类型void。
dispatcher->dispatchEvent(&event);
//消息事件回调函数
void
HelloWorld::onCustomEvent(EventCustom* event)
{
// 获取消息传递的数据
int
* data = (
int
*)event->getUserData()
CCLOG(
"onCustomEvent data = %d"
, data);
}
//
|