用Corona创建的应用在Android设备上出现错误。

在安装并在 Android 设备上运行我的应用程序后,当我点击 highscore 时,它应该发布 "highscore:0",如果这是应用程序第一次运行,我的问题是这个代码

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

似乎在 Android 设备中没有 system.DocumentsDirectory,我需要在写入问题之前先创建文本文件。因此,system.DocumentsDirectory 的替代方案是什么?因为 system.ResourceDirectory 只能读取,不能写入。

这是我用于 installation 后用户首先检查 highscore 的 highscore.lua。

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

local file = io.open( path, "r" )

local savedData = file:read( "*n" )

if (savedData == nil) then
    file=io.open(path,"w")
    local newVal="0"
    file:write(newVal)
    file:flush()
    local scoreText = display.newText("score: " .. newVal, 0, 0, "BorisBlackBloxx", 50)
    scoreText:setReferencePoint(display.CenterLeftReferencePoint)
    scoreText.x = 0
    scoreText.y = 30
else
    local scoreText = display.newText("score: " .. savedData, 0, 0, "BorisBlackBloxx", 50)
    scoreText:setReferencePoint(display.CenterLeftReferencePoint)
    scoreText.x = 0
    scoreText.y = 30
end

而这是我用于第一次玩游戏的 game.lua。

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
local reader = io.open( path, "r" )
local contents = reader:read("*n")
local file = io.open( path, "w" )

if (contents == nil) then
    local walaVal="0"
    file:write(walaVal)
    file:flush()
else
    file:write(contents)
    file:flush()
end
点赞
用户2569467
用户2569467

你能打印路径返回了什么吗?我使用DocumentsDirectory创建新的文件。

注意,许多跨平台框架普遍存在的另一个问题是,ResourceDirectory在 Android 上实际上是一个zip文件。因此访问文件更加复杂,如果需要修改文件,问题就会出现,因为不能在不先解压缩它的情况下修改zip文件中的文件。

这实际上在 Corona 的 Gotcha 部分有记录。http://docs.coronalabs.com/api/library/system/pathForFile.html

编辑 参考函数Open的API:http://docs.coronalabs.com/api/library/io/open.html 我有信心错误是因为你的标志。 使用:

file = io.open(path, "w+")

Corona的各种打开模式:

-“r”:读模式(默认);文件指针放置在文件的开头。

-“w”:只写模式;如果文件存在,则覆盖该文件。如果文件不存在,则创建一个新文件进行写入。

-“a”:附加模式(仅写);如果文件存在,则文件指针位于文件的末尾。也就是说,文件处于附加模式。如果文件不存在,则创建一个新文件进行写入。

-“r +”:更新模式(读/写),所有先前的数据都保留;文件指针将位于文件的开头。如果文件存在,只有在显式写入文件时才会被覆盖。

-“w +”:更新模式(读/写),所有先前的数据都已被删除;如果文件存在,则覆盖现有文件。如果文件不存在,则创建一个新文件进行读取和写入。

-“a +”:附加更新模式(读/写);已保留先前的数据,只允许在文件末尾进行写入。如果文件存在,则文件指针位于文件的末尾。该文件以附加模式打开。如果文件不存在,则创建一个新文件进行读取和写入。

该模式字符串也可以在末尾添加“b”,这在某些系统中需要以二进制模式打开文件。该字符串正是标准C函数fopen使用的字符串。

2013-07-24 12:31:06
用户2186639
用户2186639

你可以使用以下这些函数来非常容易地保存和加载文件。我经常用它们,完全没有遇到任何问题。

require("json")
function saveTable(t, filename)
    local path = system.pathForFile(filename, system.DocumentsDirectory)
    local file = io.open(path, "w")
    if file then
        local contents = json.encode(t)
        file:write(contents)
        io.close(file)
        return true
    else
        return false
    end
end

function loadTable(filename)
    local path = system.pathForFile(filename, system.DocumentsDirectory)
    local myTable = {}
    local file = io.open(path, "r")
    local contents = ""
    if file then
        -- read all contents of file into a string
        local contents = file:read("*a")
        myTable = json.decode(contents);
        io.close(file)
        return myTable
    end
    return nil
end

-- 与你的代码相关部分:
local scores = loadTable("scores.json")
if scores == nil then
    -- 第一次游戏
    scores = {}
    scores.highScore = 0
    saveTable(scores, "scores.json")
end
2013-07-24 14:15:22