【问题标题】:Get the name of the file a storyboard is from in Corona?获取故事板在 Corona 中的文件名?
【发布时间】:2013-01-27 20:22:09
【问题描述】:

我有一个情节提要文件,我想引用实现情节提要的文件的名称,以便加载具有相同基本名称的配置文件。

这是一个名为 scene001.lua 的文件中的示例

-- Called when the scene's view does not exist:
function scene:createScene( event )
    local screenGroup = self.view

    local json = require("json")
    -- load the book definition file
    local contents = textFromFile("scene001.json")
    local page = json.decode(contents)
    image = buildPage(page)
    screenGroup:insert(image)
    image.touch = onSceneTouch
end

如您所见,我必须对"scene001.json" 进行硬编码。我想加载与当前故事板同名的配置文件,如何动态执行此操作?

【问题讨论】:

    标签: lua coronasdk corona-storyboard


    【解决方案1】:

    您可以使用debug 库,特别是函数debug.getinfo,来检索有关当前文件名的信息。使用简单的模式,您可以将 .lua 扩展名替换为 .json 以打开您的自定义文件。

    print( debug.getinfo(1,"S").short_src ) --> /path/to/scene001.lua.
    

    【讨论】:

      【解决方案2】:

      我实际上找到了一种更好更正式的方法来执行此操作,而无需解析路径所需的所有正则表达式。使用debug.getinfo() 的潜在性能影响。

      storyboard.getCurrentSceneName()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多