有没有一种方法可以从另一个Lua文件调用和group:insert()插入对象?
2014-2-28 20:48:56
收藏:0
阅读:90
评论:2
如标题所述,我不仅想从外部的 Lua 文件调用一个对象,我还想将这个对象 group: insert() 到我的菜单页面中,并使用在外部 lua 文件中给它的属性。这个可能吗,或者有效吗?我只是想确保数据不会在我的项目中重复出现。
编辑
我到目前为止的代码是:
group:insert() 函数将抛出一个错误,说明它期望一个表格,而我可能试图调用一个函数,在这种情况下应该使用 : 而不是 .。
以下是 menu.lua:
local storyboard = require("storyboard")
local scene = storyboard.newScene()
local widget = require("widget")
local m = require("myData")
local menuFunction = require("menuFunction")
local menuSwipe
-- ========================
-- menuSwipe()
-- ========================
menuSwipe = function(self, event)
local phase = event.phase
local touchID = event.id
if (phase == "began") then
elseif (phase == "moved") then
elseif (phase == "ended" or phase == "cancelled") then
if (m.menuActivator > 0) then
menuDown(m.invisiBar, event)
else
--m.layerInfo = layers
transition.to(menuFunction.menuBar, { x = menuFunction.menuBar.x, y = 0, time = 200 })
--transition.to( layers, { x = menuFunction.menuBar.x, y = h, time = 100 } )
m.invisiBar = display.newRect(0, 0, w, 25, 6)
m.invisiBar.alpha = 0
m.menuActivator = 1
end
end
end
-- ++++++++++++++++++++++
-- menuDown()
-- ++++++++++++++++++++++
function menuDown(self, event)
local phase = event.phase
local touchID = event.id
if (phase == "began") then
elseif (phase == "moved") then
elseif (phase == "ended" or phase == "cancelled") then
if (m.menuActivator == 1) then
transition.to(menuFunction.menuBar, { x = m.menuInfo.x, y = h * 0.964, time = 200 })
--transition.to( group, { x = 0, y = 0, time = 10 } )
m.menuActivator = 0
end
end
end
function scene:createScene(event)
local group = self.view
group:insert(menuFunction.menuBar) -- *** ERROR 在这里发生
end
function scene:enterScene(event)
local group = self.view
end
function scene:exitScene(event)
local group = self.view
end
function scene:destroyScene(event)
local group = self.view
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)
return scene
这是 menuFunction.lua:
local m = require("myData")
local menu = require("menu")
local w = display.contentWidth
local h = display.contentHeight
local menuFunction = {}
--menuBar
menuFunction.menuBar = display.newImage("images/menuBar1.png")
menuFunction.menuBar.x = w * (1 / 2)
menuFunction.menuBar.y = h * 1.465
menuFunction.menuBar.height = h
menuFunction.menuBar:setReferencePoint(display.TopLeftReferencePoint)
menuFunction.menuBar.touch = menu.menuSwipe
menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar)
return menuFunction
这是实际的错误消息:
ERROR: table expected. If this is a function call, you might have used '.' instead of ':'
message**
点赞
用户487605
每次调用这个代码时是否都会出现这种情况,或者说第一次调用它时它能够工作,但第二次调用时它会崩溃 [如果在其中间删除了场景]。在你的情况下,这个代码第一次进入场景时可能可以工作,但第二次进入时可能会崩溃。
当你对一个文件进行'require'操作时,它的内容会被执行并且返回值会保存在全局包表中。当你再次要求相同的文件时,返回的值将从全局包表中取出,代码不会再次被执行。
所以如果你在应用程序的一个位置通过'require'调用此文件并且然后调用'removeSelf()'并将菜单栏的引用(nil)掉,显示对象将被删除并且它的引用将消失,再次调用'require'将不会重新创建对象。完全删除一个场景也会删除显示对象。
所以你想要实现的目标是非常明智的[与@Schollii的说法相反],但是如果你想在运行时丢弃它们,你的“模块”应该允许创建多个对象。
我不会纠正你的代码,只是一个简单的例子,说明你如何实现这个:
-- menu.lua
local menuCreator = {}
menuCreator.newMenu = function(params)
local menu = display.newGroup()
-- create your menu here
return menu
end
return menuCreator
现在,无论何时你执行:
local menuCreator = require("menu.lua")
你都可以调用:
local menu = menuCreator.newMenu(someParams)
并在需要的任何地方获得一个漂亮的新菜单。如果它不总是在屏幕上显示,那么在你需要它的时候创建一个新的可能会更好,然后从内存中删除它。
2014-07-26 04:29:38
评论区的留言会收到邮件通知哦~
推荐文章
- 如何将两个不同的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中获取用户配置主目录的跨平台方法
有几个问题,似乎都与您的错误无关,但修复它们也会修复错误或使错误的原因更加明显。请修复以下内容并更新:
虽然Lua允许,但不要使用循环包含,其中A包含B,B又包含A。相反,让“menu”需要“menuFunction”,然后调用“menuFunction”中的创建函数:
-- menuFunction.lua local m = require("myData") -- require("menu") -- BAD! :) local w = display.contentWidth local h = display.contentHeight local menuBar = display.newImage("images/menuBar1.png") menuBar.x = w * (1/2) menuBar.y = h * 1.465 menuBar.height = h menuBar:setReferencePoint(display.TopLeftReferencePoint) local menuFunction = { menuBar = menuBar } function createMenuBar(menuSwipe) menuFunction.menuBar.touch = menuSwipe menuFunction.menuBar:addEventListener("touch", menuFunction.menuBar) return menuFunction end -- menu.lua function createScene(event) local mf = require('menuFunction') mfFunction = mf.createMenuBar(menuSwipe) group:insert(menuFunction.menuBar) end其次,在对“group:insert()”的四次调用中,前3个引用的对象在代码中未显示,并且不相关于问题,应删除它们;如果您认为相关,请注释为什么它们的代码未显示,或显示它们的代码。