加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Lua回调函数小结

发布时间:2020-12-14 22:06:50 所属栏目:大数据 来源:网络整理
导读:本文转自:http://www.zaojiahua.com/lua-callback-functions.html? 感谢作者的分享 1、菜单按钮的回调。这二者的回调是这么实现的,新建一个菜单或者是按钮,为了点击菜单或者按钮以后实现程序的逻辑,我们需要为菜单和按钮来绑定一个 回调函数 ,于是乎,

本文转自:http://www.zaojiahua.com/lua-callback-functions.html? 感谢作者的分享


1、菜单按钮的回调。这二者的回调是这么实现的,新建一个菜单或者是按钮,为了点击菜单或者按钮以后实现程序的逻辑,我们需要为菜单和按钮来绑定一个回调函数,于是乎,我们有了以下的代码。

1 --定义菜单项的回调函数
2 ????local?function?item1_callback()
3 ????????--切换场景
4
????????local?gameScene =?require("GameScene")
5 ????????cc.Director:getInstance():replaceScene(gameScene:createScene())
6 end
7 item2_callback()
8 --切换场景
9 aboutScene =?"AboutScene")
10 cc.Director:getInstance():pushScene(aboutScene:createScene())
11 end
12 item1 = cc.MenuItemLabel:create(cc.Label:createWithTTF("开始游戏","fonts/menu.ttf"
13 ????item1:registerScriptTapHandler(item1_callback)
14 item2 = cc.MenuItemLabel:create(cc.Label:createWithTTF("关于游戏"15 item2:registerScriptTapHandler(item2_callback)
16 ????--创建菜单
17 menu = cc.Menu:create(item1,item2)
18 menu:alignItemsVerticallyWithPadding(size.height*0.15)
19 ?
20 --添加菜单到游戏中
21 layer:addChild(menu)

在这里我们使用了一个重要的回调注册函数registerScriptTapHandler,Tap就是按下的意思,所以调用这个函数的时候是对菜单和按钮进行绑定的,这个函数的调用者是菜单项,需要传入一个参数,这个参数就是绑定的函数。接下来我们看下绑定的函数,我绑定的函数并没有接受任何传递过来的参数,这个就是Lua,你可以选择接受或者不接受,但是参数就在那里。为了看看这货是什么鸟,我们写上俩个参数,打印一下,发现一个值是-1,一个值是UserData,有图为证。

item1_callback(para1,para2)
????????print(para1,para2)
cc.Director:getInstance():replaceScene(gameScene:createScene())
end

屏幕快照 2014-09-13 下午3.23.06

打印的结果是-1和userdata,userdata我们可以理解,你绑定的谁那么就传递过来谁嘛,但是第一个传递过来的参数是-1肿么回事?原来第一个参数代表的是tag,如果你没有给你的菜单项设置tag,那么它就是-1,如果设置了tag,那么这个值就是tag的值,OK,这样的话就清楚了。以下的代码是添加button的代码,一共添加了三个button,大家可以参考一下。

--添加按钮
GameScene:addButton(layer)
--创建button
scale9_normal()
return?cc.Scale9Sprite:create("buttonBackground.png"scale9_press()
"buttonHighlighted.png"--文本信息
self.button1_label = cc.Label:createWithTTF("Ready""fonts/label.TTF"self.button2_label = cc.Label:createWithTTF(
self.button3_label = cc.Label:createWithTTF(--button1
button1 = cc.ControlButton:create(self.button1_label,scale9_normal())
button1:setBackgroundSpriteForState(scale9_press(),cc.CONTROL_EVENTTYPE_TOUCH_DOWN)
button1:setTag(1)
button1:setPosition(self.size.width*0.2,self.size.height*0.2)
--button2
button2 = cc.ControlButton:create(self.button2_label,scale9_normal())
button2:setBackgroundSpriteForState(scale9_press(),cc.CONTROL_EVENTTYPE_TOUCH_DOWN)
22 button2:setTag(2)
23 button2:setPosition(self.size.width*0.5,self.size.height*0.2)
24 --button3
25 button3 = cc.ControlButton:create(self.button3_label,monospace!important; border:0px!important; font-size:1em!important; padding:0px 0.3em 0px 0px!important; margin:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:2.7em!important; line-height:1.1em!important; direction:ltr!important; display:block!important; background:none!important">26 button3:setBackgroundSpriteForState(scale9_press(),monospace!important; border:0px!important; font-size:1em!important; padding:0px 0.3em 0px 0px!important; margin:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:2.7em!important; line-height:1.1em!important; direction:ltr!important; display:block!important; background:none!important">27 button3:setTag(3)
28 button3:setPosition(self.size.width*0.8,monospace!important; border:0px!important; font-size:1em!important; padding:0px 0.3em 0px 0px!important; margin:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:2.7em!important; line-height:1.1em!important; direction:ltr!important; display:block!important; background:none!important">29 30 --设置button的回调函数
31 button_callback =?(ref,button)
32 --如果正确跟新分数
33 if?self.correct == ref:getTag()?then
34 ????????????self:correct_callback()
35 else
36 self:wrong_callback()
37 38 39 40 --设置回调函数
41 button1:registerControlEventHandler(button_callback,cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE)
42 button2:registerControlEventHandler(button_callback,cc.CONTROL_EVENTTYPE_TOUCH_UP_INSIDE)
43 button3:registerControlEventHandler(button_callback,monospace!important; border:0px!important; font-size:1em!important; padding:0px 0.3em 0px 0px!important; margin:0px!important; outline:0px!important; text-align:right!important; float:none!important; vertical-align:baseline!important; position:static!important; left:auto!important; top:auto!important; right:auto!important; bottom:auto!important; height:auto!important; width:2.7em!important; line-height:1.1em!important; direction:ltr!important; display:block!important; background:none!important">44 ?
45 --添加到层中
46 layer:addChild(button1)
47 layer:addChild(button2)
48 layer:addChild(button3)
49 2、添加Android返回键的响应代码,Android返回键同样需要回调函数来做这件事情,在Cocos中使用的是事件监听和分发机制,所以我们需要先注册一下事件类型,然后绑定回调,代码如下。

--监听手机返回键
key_listener = cc.EventListenerKeyboard:create()
--返回键回调
key_return()
--结束游戏
cc.Director:getInstance():endToLua()
--lua中得回调,分清谁绑定,监听谁,事件类型是什么
key_listener:registerScriptHandler(key_return,cc.Handler.EVENT_KEYBOARD_RELEASED)
eventDispatch = layer:getEventDispatcher()
eventDispatch:addEventListenerWithSceneGraphPriority(key_listener,layer)

这次我们使用的注册回调函数是registerScriptHandler,没有Tap,显然就不是为菜单和按钮回调准备的,所以这个函数是用来注册一般的回调函数所使用的。其中注释有句话说的比较好,就是一定要明白谁绑定,监听谁,事件类型是什么,在这个例子看来,是事件监听器去绑定了一个回调函数key_return,然后事件的类型是放到了一个Handler表中的,我们的事件类型是keyBoard,可以到Handler中看看都有哪些事件类型,然后所有你熟悉的东西都会看到了,比如以下的代码所代表的事件类型。

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 )
eventDispatcher = layerFarm:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener,layerFarm)

3、动作的回调,3.x以后在c++层的动作回调函数只剩下Callfunc和CallfuncN了,其他的都可以用c++11的std::bind解决,而在Lua中就剩下一个了,这个是由Lua的特性决定的,用法如下。

--ready和go的回调函数 函数可以有俩个参数,第一个代表接受动作的对象,就是谁执行了动作,第二个是传过来的参数,全部放到了表中
local??ready_action =?function(actionSelf,tab)
go_action =?()
cc.CallFunc:create(ready_action,{x=1})

CallFunc中有俩个参数,一个是绑定的回调函数,另一个是table表,这个表中可以存放一些命名参数,把你想要传递的东西统统通过这个表传递过去,然后在回调函数中,我们有俩个参数来接受,第一个当然是动作的执行者了,第二个就是传递过来的这张表,里边有所有我们要得参数,是不是很高大上啊!

4、看完了以上的这些东西,我们基本明白了怎么使用,所谓知其然更要知其所以然,这些回调函数是怎么分发到Lua层的代码中的,我们就需要看一个文件了。打开你引擎目录的cocos/scripting/lua-bindings/manual/CCLuaEngine.cpp文件,我们需要找到这样一处代码。

int?LuaEngine::sendEvent(ScriptEvent* evt)
{
????if?(NULL == evt)
????????return?0;
switch?(evt->type)
????{
case?kNodeEvent:
???????????????????????????handleNodeEvent(evt->data);
}
????????????break;
kMenuClickedEvent:
????????????????handleMenuClickedEvent(evt->data);
}
;
kCallFuncEvent:
handleCallFuncActionEvent(evt->data);
kScheduleEvent:
handleScheduler(evt->data);
kTouchEvent:
handleTouchEvent(evt->data);
kTouchesEvent:
handleTouchesEvent(evt->data);
kKeypadEvent:
handleKeypadEvent(evt->data);
kAccelerometerEvent:
handleAccelerometerEvent(evt->data);
kCommonEvent:
50 handleCommonEvent(evt->data);
51 52 53 kControlEvent:
54 55 handlerControlEvent(evt->data);
56 57 58 default:
59 60 61 62 63 }

sendEvent顾名思义,这段代码就是向Lua层来分发回调事件的代码,在这段代码中,我们看到了很多熟悉的身影,比如说node的分发,menu的分发,schedule的分发,touch的分发等等都在这里,但是却没有事件监听器的分发,没错,事件监听器的回调Lua并没有写在这个文件中,而是在其他的文件中处理的,大家可以自行研究。我们以node回调Lua层的代码为例,看看是怎么回调Lua层的函数的,关键的代码在这里。

action = *((int*)(basicScriptData->value));
(action)
kNodeOnEnter:
_stack->pushString("enter");
kNodeOnExit:
"exit"kNodeOnEnterTransitionDidFinish:
"enterTransitionFinish"kNodeOnExitTransitionDidStart:
"exitTransitionStart"kNodeOnCleanup:
"cleanup"0;
????ret = _stack->executeFunctionByHandler(handler,1);
_stack->clean();

根据action的类型,我们将不同的字符串压入了栈中,然后执行了executeFunctionByHandler函数,第一个参数handler就是回调的函数,第二个参数代表传递到回调函数中的参数个数。其他的回调Lua层的函数大同小异,大家自行研究吧。那么在Lua层,这个回调函数是怎么被注册的呢?我们新建工程由系统创建了一个GameScene.lua文件,这个文件中的这段代码,不知道大家是否留意过。

查看源代码
打印 帮助
onNodeEvent(event)
if?"exit"?== event?then
cc.Director:getInstance():getScheduler():unscheduleScriptEntry(self.schedulerID)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读