0

在我的应用程序中,我的结构类似于以下内容:

app (baseDirectory)
 |
 +-> config
      |
      +-> Levels

使用电晕时,我试图自动加载 Levels 目录中的所有文件。以下是我目前的做法。注意:这确实适用于 Windows,但不适用于 mac。

local dirPath = system.pathForFile('config/Levels')
for file in lfs.dir(dirPath) do
    if (file ~= '.' and file ~= '..') then
        -- require the file
    end
end

现在,如果我使用以下内容,它适用于 Mac,但不适用于“config/Levels”。

local dirPath = system.pathForFile('config')

我不确定这是一个错误还是我做错了什么。我会假设因为它适用于 Windows 但不适用于 Mac,这将是一个错误。


所以总而言之,我怎样才能让以下内容与上述目录结构一起使用

local dirPath = system.pathForFile('config/Levels')
4

2 回答 2

1

经过多次调试,我找到了 Mac 的解决方案。下面是我在目录中获取文件的函数。适用于 Windows 和 Mac。

在 baseDirectory 中必须有一个名为 main.lua 的文件。

-- path is relative to the baseDirectory
function getFiles(path)
    local filenames = { }
    local baseDir = system.pathForFile('main.lua'):gsub("main.lua", "")
    for file in lfs.dir(baseDir .. path) do
        if (file ~= '.' and file ~= '..') then
            table.insert(filenames, file)
        end
    end
    if (#filenames == 0) then return false end
    return filenames
end

问题是 system.pathForFile('directory/subdirectory') 在 Mac 上不起作用,因为它正在严格查找文件。只要它可以在 baseDirectory 中找到某个文件,您就可以在相对路径上附加以检索预期目录中的所有文件。

于 2013-06-01T06:42:14.120 回答
0

一种解决方案是更改“级别”中的“级别”。也许它是区分大小写的。第二种解决方案是不使用配置文件夹中的“级别”目录。以下是我建议的组织文件的方法:

 app (baseDirectory)  
 |  
 +-> • configLevels  
     • configPlayer  
     • configOther
于 2013-05-31T09:33:25.810 回答