【问题标题】:App created in corona gets an error in android device在电晕中创建的应用程序在 android 设备中出现错误
【发布时间】:2013-07-23 22:14:15
【问题描述】:

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

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

Android 设备中似乎没有 system.DocumentsDirectory 我需要在写入之前创建文本文件拳头问题是我的路径我需要它来创建 myfile.text 那么什么可以替代 system.DocumentsDirectory?我不能使用 system.ResourceDirectory 导致它只可读不可写

这是用于我的 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

【问题讨论】:

  • 你的App有权限访问存储吗?

标签: android lua coronasdk


【解决方案1】:

你能打印出路径返回的内容吗?我使用DocumentsDirectory 创建新文件。

请注意,如果不是所有多平台框架,还有一个常见的问题是ResourceDirectory 实际上是 Android 上的 zip。因此访问文件更加复杂,如果您需要修改文件,就会出现问题,因为如果不先解压缩就无法修改 zip 中的文件。 这实际上记录在 Corona 的 Gotcha 部分。 http://docs.coronalabs.com/api/library/system/pathForFile.html

编辑 参考API函数打开:http://docs.coronalabs.com/api/library/io/open.html 我确信错误是由于您的 flag 造成的。 采用: file = io.open(path, "w+")

Corona 的各种打开模式:

  • “r”:读取模式(默认);文件指针位于 文件的开头。

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

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

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

  • “w+”:更新模式(读/写),所有之前的数据都被擦除;如果文件存在,则覆盖现有文件。如果文件没有 存在,创建一个新的文件进行读写。

  • “a+”:追加更新模式(读/写);保留以前的数据,只允许在文件末尾写入。文件 如果文件存在,则指针位于文件末尾。文件打开 在追加模式下。如果文件不存在,则创建一个新文件 用于阅读和写作。

模式字符串的末尾也可以有一个'b',在某些系统中以二进制模式打开文件时需要它。这个字符串正是标准 C 函数 fopen 中使用的字符串。

【讨论】:

  • 我已经在我的安卓设备上安装了我的游戏,并且我正在为我的场景使用导演类 你使用了这个代码 local alert = native.showAlert( "Director Class - ERROR",debugMessage, { "OK" }, onComplete ),这样当我在我的设备上运行应用程序时,它会打印出什么问题,它说文件返回一个 nil 值,其中我的文件等于 file=io.open(path,"r") 所以这就是为什么我知道问题在于路径
  • 检查我的编辑,我很漂亮这是因为你使用“r”作为你的开放标志。
  • 是的,我用它来写我的高分我有 file=io.open(path,"r") 的原因是检查我的 txt 文件中是否有值,如果它为零如果 nil 将创建值 0,则写入以前的高分
  • 我不知道如何帮助你了。我认为您正在做正确的事情来检查文件是否存在。如果 file 为 nil,那么您应该使用 io.open(path, "w") 重新打开它来创建它。那么你的文件句柄不应该是零。也许在你的问题中添加一些代码来澄清?
【解决方案2】:

您可以使用这些功能非常轻松地保存和加载文件。我一直与他们合作,完全没有遇到任何问题。

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


    -- The part related to your code :
    local scores = loadTable( "scores.json" )
    if scores == nil then
        -- First time in the game
        scores = {}
        scores.highScore = 0
        saveTable( scores, "scores.json" )
    end

【讨论】:

    猜你喜欢
    • 2014-10-04
    • 2015-07-21
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多