在Lua中创建函数
发布时间:2020-12-15 00:27:36 所属栏目:大数据 来源:网络整理
导读:当我通过分配创建函数时,“if”条件不起作用,但是当我创建函数时,如下面的第二个示例,它可以工作.你能告诉我为什么吗? 不工作: local start=os.time()local countDown = function(event) if((os.time()-start)==3) then Runtime: removeEventListener("ent
当我通过分配创建函数时,“if”条件不起作用,但是当我创建函数时,如下面的第二个示例,它可以工作.你能告诉我为什么吗?
不工作: local start=os.time() local countDown = function(event) if((os.time()-start)==3) then Runtime: removeEventListener("enterFrame",countDown) end print(os.time()-start) end Runtime:addEventListener("enterFrame",countDown) 工作: local start=os.time() local function countDown(event) if((os.time()-start)==3) then Runtime: removeEventListener("enterFrame",countDown) 解决方法
那是因为当你执行本地countDown = …时,countDown变量在……部分执行之后才存在.因此,您的函数将访问全局变量,而不是尚不存在的本地变量.
请注意,Lua将本地函数countDown …转换为以下内容: local countDown countDown = function ... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |