ios - 在 iOS 上使用 Corona 时,我可以将我的游戏信息分成两个数据库吗?

标签 ios sqlite coronasdk

我正在使用 Corona SDK 创建一个游戏,该 SDK 使用 SQLite 数据库来获取游戏信息,例如不同世界的图像文件位置和各个级别的布局信息。显然我也希望存储游戏进度(例如,关卡的高分、保存当前关卡状态、哪些关卡已解锁、应用内购买数据等)

我计划执行以下操作,并且这是我真正需要专家意见的地方:

• Deploy two SQLite databases with my app. One that has the game setup data (image files, world names, etc), and one that will store the player progress data. The player progress db will have a couple rows prepopulated to indicate which levels are unlocked from the start.

• Copy the game setup db to the Caches Directory, and copy the player progress db to the Documents Directory.

是否有更好或更有效的方法来做到这一点? Corona 不支持 iOS 中的“不备份”标志,因此我的缓存数据库有可能因内存不足而被清除,我将不得不添加额外的代码来检查它是否存在或重新创建它。此外,在单独的数据库上执行 SQL 操作(例如联接)也有点痛苦。

最佳答案

我已经使用 Corona 创建了应用程序。您可以使用 Documents 文件夹来存储游戏进度。你不应该在那里存储的是游戏 Assets 等等。 Assets 和代码不得放在特定的文件夹中:您可以直接在应用程序的“根”文件夹中访问它们,而无需介绍文件夹名称。在那里,您可以放置​​您想要在游戏中使用的任何东西,这些东西在应用程序的使用过程中不会改变。 所以基本上,除非你打算做更多特别的事情,否则你会:

  • 加载图像、代码和 sqlite 数据库,您不会仅使用它们的名称来更改它们。例如:local picture = display.newImageRect("mypic.png", 40, 47)
  • 将进度数据加载并保存到 Documents 文件夹中:filedata:copyFile( "scores.db", system.ResourceDirectory, "scores.db", system.DocumentsDirectory ,0)

这将是 filedata:copyFile 函数的内容:

function filedata:copyFile( srcName, srcPath, dstName, dstPath, overwrite )

local results = true                -- assume no errors

-- Copy the source file to the destination file
--
local rfilePath = system.pathForFile( srcName, srcPath )
local wfilePath = system.pathForFile( dstName, dstPath )

local rfh = io.open( rfilePath, "rb" )              
local wfh = io.open( wfilePath, "rb" )
if wfh~=nil then
    if overwrite==0 then
        results=false
        return
    else
    end
end
--io.close(wfh)
local wfh = io.open( wfilePath, "wb" )

if  not wfh then
    results = false                 -- error
else
    -- Read the file from the Resource directory and write it to the destination directory
    local data = rfh:read( "*a" )

    if not data then
        --print( "read error!" )
        results = false     -- error
    else
        if not wfh:write( data ) then
            --print( "write error!" ) 
            results = false -- error
        end
    end
end
    -- Clean up our file handles
    rfh:close()
    wfh:close()

    return results  
end

而且您将始终访问进度数据库并将其内容保存到 Documents 目录中。 如您所见,这比您的建议要容易得多:)

关于ios - 在 iOS 上使用 Corona 时,我可以将我的游戏信息分成两个数据库吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20226417/

相关文章:

object - 在 Lua 中,如何找出存储对象的键?

objective-c - 使用 Testflight 不接收崩溃

ios - 环境对象内的 SwiftUI 可观察对象不更新 View

ios - FMDB更新版本号不起作用

ios - 从 TableView 到标签的数组数据

sql - 从 SQL 表中删除重复的行

lua - 为什么我的球不旋转? - lua - 电晕

ios - 如何发现 2 CGPath roteted rects 是否相交?

ios - 提取 JSON 并将其保存在 NSMutableArray 中

audio - 电晕:重新创建场景后,音频不会再开始播放