coocs2dx 版本 3.1.1
registerScriptTouchHandler 注册触屏事件
registerScriptTapHandler 注册点击事件
registerScriptHandler 注册基本事件 包括 触屏 层的进入 退出 事件
registerScriptKeypadHandler 注册键盘事件
registerScriptAccelerateHandler 注册加速事件
registerScriptTouchHandler 详解(可以设置单点或多点)
function gameWindow:addLayerTouchEventMethod1()
local function onTouchEvent(eventType, x, y)
--log("eventType = "..tostring(eventType))
if eventType == "began" then
--需要返回true
return onTouchBegan(touch, event)
elseif eventType == "moved" then
onTouchMoved(touch, event)
elseif eventType == "ended" then
onTouchEnded(touch, event)
end
end
config.bottomLayer:setTouchEnabled(true)
config.bottomLayer:registerScriptTouchHandler(onTouchEvent)
end
registerScriptTapHandler 注册点击事件
function gameWindow:addBtn()
local btn = cc.MenuItemImage:create("white.png", "black.png", "black.png")
btn:setPosition(320, 160)
local function btnClick()
log("btnClick")
end
btn:registerScriptTapHandler(btnClick)
local menu = cc.Menu:create()
config.bottomLayer:addChild(menu)
menu:setPosition(cc.p(0,0))
menu:addChild(btn)
end
registerScriptHandler 注册基本事件
注册触屏事件用法
function gameWindow:addLayerTouchEventMethod2()
--创建一个单点触屏事件
local listener = cc.EventListenerTouchOneByOne:create()
--注册触屏开始事件
listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
--注册触屏移动事件
listener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED)
--注册触屏结束事件
listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED)
--获取层的事件派发器
local eventDispatcher = config.bottomLayer:getEventDispatcher()
--事件派发器 注册一个node事件
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, config.bottomLayer)
end
注册layer的 进入 退出事件用法
function gameWindow:addLayerEnterAndExitEvent()
local function onNodeEvent(eventType)
if eventType == "enter" then
log("enter")
elseif eventType == "exit" then
log("exit")
end
end
config.bottomLayer:registerScriptHandler(onNodeEvent)
end
而在3.x中由于加入了C++11的特性,而对事件的分发机制通过事件分发器 EventDispatcher 来进行统一的管理。
事件监听器主要有:
> 触摸事件 : EventListenerTouchOneByOne 、EventListenerTouchAllAtOnce
> 鼠标响应事件 : EventListenerMouse
> 键盘响应事件 : EventListenerKeyboard
> 加速计事件 : EventListenerAcceleration
> 自定义事件 : EventListenerCustom
> 物理碰撞事件 : EventListenerPhysicsContact
> 游戏手柄事件 : EventListenerController
【致谢】
http://www.cnblogs.com/Richard-Core/p/3892641.html
http://blog.csdn.net/runaying/article/details/16352879
【事件分发器】
事件分发器EventDispatcher ,用于统一管理事件监听器的所有事件的分发。
1、_eventDispatcher
_eventDispatcher 是Node的属性,通过Director::getInstance()->getEventDispatcher() 获得。
_eventDispatcher的工作由三部分组成:
(1)事件分发器 : EventDispatcher。
(2)事件类型 : EventTouch, EventKeyboard 等。
(3)事件监听器 : EventListenerTouch, EventListenerKeyboard 等。
监听器实现了各种触发后的逻辑,在适当时候由事件分发器分发事件类型,然后调用相应类型的监听器。
2、添加/删除监听器
添加监听器: addEventListenerWithSceneGraphPriority ,
addEventListenerWithFixedPriority 。
删除监听器: removeEventListener ,
removeAllEventListeners 。
3、主要函数
包含监听器的添加、删除、暂停、恢复,优先级的设置,手动分发事件等。
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
{
void
addEventListenerWithSceneGraphPriority(EventListener* listener, Node* node);
void
addEventListenerWithFixedPriority(EventListener* listener,
int
fixedPriority);
EventListenerCustom* addCustomEventListener(
const
std::string &eventName,
const
std::function<
void
(EventCustom*)>& callback);
void
removeEventListener(EventListener* listener);
void
removeEventListenersForType(EventListener::Type listenerType);
void
removeEventListenersForTarget(Node* target,
bool
recursive =
false
);
void
removeCustomEventListeners(
const
std::string& customEventName);
void
removeAllEventListeners();
void
pauseEventListenersForTarget(Node* target,
bool
recursive =
false
);
void
resumeEventListenersForTarget(Node* target,
bool
recursive =
false
);
void
setPriority(EventListener* listener,
int
fixedPriority);
void
setEnabled(
bool
isEnabled);
bool
isEnabled()
const
;
void
dispatchEvent(Event* event);
void
dispatchCustomEvent(
const
std::string &eventName,
void
*optionalUserData = nullptr);
}
4、关于事件监听器的优先权
通过 addEventListenerWithSceneGraphPriority 添加的监听器,优先权为0。
通过 addEventListenerWithFixedPriority 添加的监听器,可以自定义优先权,但不能为0。
> 优先级越低,越先响应事件。
> 如果优先级相同,则上层的(z轴)先接收触摸事件。
5、使用步骤
(1)获取事件分发器 : dispatcher = Director::getInstance()->getEventDispatcher();
(2)创建监听器 : auto listener = EventListenerTouchOneByOne::create();
(3)绑定响应事件函数: listener->onTouchBegan = CC_CALLBACK_2(callback, this);
(4)将监听器添加到事件分发器dispatcher中:
dispatcher->addEventListenerWithSceneGraphPriority(Listener, this);
(5)编写回调响应函数:
bool callback(Touch* touch, Event* event) { ... }
【触摸事件】
1、单点触摸:EventListenerTouchOneByOne
单点触摸监听器相关:
1
2
3
4
5
6
7
8
static
EventListenerTouchOneByOne* create();
std::function<
bool
(Touch*, Event*)> onTouchBegan;
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();
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
);
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"
); }
2、多点触摸:EventListenerTouchAllAtOnce
多点触摸监听器相关:
1
2
3
4
5
6
7
8
static
EventListenerTouchAllAtOnce* create();
std::function<
void
(
const
std::vector<Touch*>&, Event*)> onTouchesBegan;
std::function<
void
(
const
std::vector<Touch*>&, Event*)> onTouchesMoved;
std::function<
void
(
const
std::vector<Touch*>&, Event*)> onTouchesEnded;
std::function<
void
(
const
std::vector<Touch*>&, Event*)> onTouchesCancelled;
使用举例:
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();
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
);
dispatcher->addEventListenerWithSceneGraphPriority(touchesListener,
this
);
void
onTouchesBegan(
const
std::vector<Touch*>& touches, Event *unused_event) { CCLOG(
"began"
); }
void
onTouchesMoved(
const
std::vector<Touch*>& touches, Event *unused_event) { CCLOG(
"moved"
); }
void
onTouchesEnded(
const
std::vector<Touch*>& touches, Event *unused_event) { CCLOG(
"ended"
); }
void
onTouchesCancelled(
const
std::vector<Touch*>&touches, Event *unused_event) { CCLOG(
"cancelled"
); }
【鼠标事件】
EventListenerMouse ,主要用于监听鼠标的点击、松开、移动、滚轮的事件。
鼠标事件监听器相关:
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* 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
);
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"
); }
【键盘事件】
EventListenerKeyboard ,主要用于监听键盘某个键的按下、松开的事件。
键盘事件监听器相关:
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;
使用举例:
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"
);
}
}
【加速计事件】
EventListenerAcceleration ,主要用于监听移动设备的所受重力方向感应 事件。
重力感应来自移动设备的加速计,通常支持 (X, Y, Z) 三个方向的加速度感应,所以又称为三向加速计。在实际应用中,可以根据3个方向的力度大小来计算手机倾斜的角度或方向。
1、加速计信息类Acceleration
该类中每个方向的加速度,大小都为一个重力加速度大小。
1
2
3
4
5
6
class
Acceleration
{
double
x;
double
y;
double
z;
};
2、开启加速计感应
在使用加速计事件监听器之前,需要先启用此硬件设备:
Device::setAccelerometerEnabled(true);
3、加速计监听器相关
1
2
3
4
5
static
EventListenerAcceleration* create(
const
std::function<
void
(Acceleration*, Event*)>& callback);
std::function<
void
(Acceleration*, Event*)> onAccelerationEvent;
4、使用举例
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
请发表评论