如何将整个函数插入到组中(场景组)
2015-7-14 4:55:15
收藏:0
阅读:100
评论:1
我知道如何将单独的变量放入一组,但如果我需要在组中添加一个函数(当我想要创建一个场景时,我可以将整个函数插入到该“sceneGroup”中,而不是逐个输入每个变量),那么我如何插入它?
代码:
function scene:create(event)
local sceneGroup = self.view
end
function scene:show(event)
local sceneGroup = self.view
local phase = event.phase
if (phase == "will") then
sceneGroup:insert(HomePage()) --- 这就是我尝试过的方法
elseif (phase == "did") then
end
end
scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)
return scene
scene1.lua:140: ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
这是我得到的错误消息。
我也尝试过
sceneGroup:insert(HomeGroup) --- 这没有在末尾加 (),但仍然无法正常工作。
请告诉我您有什么想法或知道如何做到这一点。
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?
- addEventListener 返回 nil Lua
- Lua中获取用户配置主目录的跨平台方法
你无法将函数添加到显示组。由于
sceneGroup是一个表格(就像lua中的大多数东西一样),你可以像这样声明HomePage:sceneGroup.HomePage = function(params) -- HomePage的代码 end如果你想通过
composer.setVariable和composer.getVariable向composer添加一个函数,你可以选择这种方法。local composer = require( "composer" ) local scene = composer.newScene() -- loclas local testFunc = function(hello) print(hello) end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view --创建一个名为“myFunction”的变量,其值为对testFunc的引用 composer.setVariable( "myFunction", testFunc ) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "did" ) then -- 当场景现在在屏幕上时调用。 composer.getVariable( "myFunction" )("Testing!") end end -- 侦听器设置 scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) return scene这将在场景完成动画上屏后输出“Testing”。
然而,我看不到这样做的目的。如果你想为composer场景添加一个函数,我建议使用这种方法。
local composer = require( "composer" ) local scene = composer.newScene() -- 函数的本地向前引用应在这里 local myFunction -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- 在这里初始化函数 myFunction = function(param) print("myFunction says "..param) end end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "did" ) then -- 当场景现在在屏幕上时调用。 myFunction("hello") end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- 在这里移除你场景的函数 myFunction = nil end -- 侦听器设置 scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "destroy", scene ) return scene正如你所看到的,无论何时都可以调用
myFunction。这个例子将在场景完成动画上屏后打印“myFunction says hello”。希望有所帮助,
乔。