cocos2dx+lua注册事件函数详解
coocs2dx 版本 3.1.1
registerScriptTouchHandler 注册触屏事件
registerScriptTapHandler 注册点击事件
registerScriptHandler 注册基本事件 包括 触屏 层的进入 退出 事件
registerScriptKeypadHandler 注册键盘事件
registerScriptAccelerateHandler 注册加速事件
registerScriptTouchHandler 详解(可以设置单点或多点)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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 注册点击事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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 注册基本事件
注册触屏事件用法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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的 进入 退出事件用法
1
2
3
4
5
6
7
8
9
10
|
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 |
registerScriptHandler 注册键盘事件
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
|
function cocoEvent.addKeyboardEvent() local function keyboardPressed(keyCode, event) if keyCode == 23 then log( "left" ) elseif keyCode == 24 then log( "right" ) elseif keyCode == 25 then log( "up" ) elseif keyCode == 26 then log( "down" ) end --log( "keyCode = " ..tostring(keyCode)) --log( "event = " ..tostring(event)) end local function keyboardReleased(keyCode, event) --log( "keyCode = " ..tostring(keyCode)) --log( "event = " ..tostring(event)) end local listener = cc.EventListenerKeyboard:create() listener:registerScriptHandler(keyboardPressed, cc.Handler.EVENT_KEYBOARD_PRESSED) listener:registerScriptHandler(keyboardReleased, cc.Handler.EVENT_KEYBOARD_RELEASED) local eventDispatcher = config.bottomLayer:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener, config.bottomLayer) end |