1

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

这是一个名为的文件中的示例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". 我想加载一个与当前故事板同名的配置文件,我该如何动态地做到这一点?

4

2 回答 2

2

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

print( debug.getinfo(1,"S").short_src ) --> /path/to/scene001.lua.
于 2013-01-27T21:32:41.347 回答
0

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

storyboard.getCurrentSceneName()

于 2013-01-29T06:30:52.500 回答