Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android updateScore不工作_Android_Lua_Coronasdk - Fatal编程技术网

Android updateScore不工作

Android updateScore不工作,android,lua,coronasdk,Android,Lua,Coronasdk,我的updateScore有个问题,我的功能是如果用户第一次玩游戏 它将创建名为myFile.txt的文件来记录分数,现在要做的代码是(如果reader then)查看文件是否存在,如果不存在,它将转到我的或者如果已经有一个文件,那么我的内容应该有一个分数值,然后我可以使用它来比较并获得我的高分 问题是我的内容总是返回nil值,因此当你玩游戏时你总是得到的分数将取代我的高分,我不知道我做错了什么 这是我的密码 function updateScore() local path = sy

我的updateScore有个问题,我的功能是如果用户第一次玩游戏

它将创建名为myFile.txt的文件来记录分数,现在要做的代码是(如果reader then)查看文件是否存在,如果不存在,它将转到我的或者如果已经有一个文件,那么我的内容应该有一个分数值,然后我可以使用它来比较并获得我的高分

问题是我的内容总是返回
nil
值,因此当你玩游戏时你总是得到的分数将取代我的高分,我不知道我做错了什么

这是我的密码

function updateScore()

    local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
    local reader = io.open( path, "r" )
    local file = io.open( path, "w" )

    if reader then

        reader:close()
        local reader1 = io.open( path, "r" )
        local contents = reader1:read("*n")


        if (stopscore == false) then
            score = score + 1
            scoreText.text = "score: " .. score
            scoreText:setReferencePoint(display.CenterLeftReferencePoint)
            scoreText.x = 0
            scoreText.y = 30
        end

        if (stopscore == true) then

            if (contents == nil) then
                local file = io.open( path, "w" )
                file:write(score)
                file:flush()
                file:close()
                timer.pause(timer1)
                director:changeScene( "menu", "downFlip" )

            else

                if (contents < score) then
                    file:write(score)
                    file:flush()
                    file:close()
                    timer.pause(timer1)
                    director:changeScene( "menu", "downFlip" )
                else
                    file:write(contents)
                    file:flush()
                    file:close()
                    timer.pause(timer1)
                    director:changeScene( "menu", "downFlip" )
                end

            end
        end

    else

        local file1 = io.open( path, "w" )
        local walaVal=0
        file1:write(walaVal)
        file1:close()

        if (stopscore == false) then
            score = score + 1
            scoreText.text = "score: " .. score
            scoreText:setReferencePoint(display.CenterLeftReferencePoint)
            scoreText.x = 0
            scoreText.y = 30
            print(contents)
        end

        if (stopscore == true) then
            local file = io.open( path, "w" )
            file:write(score)
            file:flush()
            file:close()
            timer.pause(timer1)
            director:changeScene( "menu", "downFlip" )
        end

    end
end
函数updateScore()
本地路径=system.pathForFile(“myfile.txt”,system.documents目录)
本地读卡器=io.open(路径“r”)
本地文件=io.open(路径“w”)
如果读者
读者:关闭()
本地读取器1=io.open(路径“r”)
本地内容=reader1:已读(“*n”)
如果(stopscore==false),则
分数=分数+1
scoreText.text=“分数:。。分数
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x=0
scoreText.y=30
结束
如果(stopscore==true),则
如果(contents==nil),则
本地文件=io.open(路径“w”)
文件:写入(分数)
文件:flush()
文件:close()
计时器。暂停(计时器1)
导演:变更场景(“菜单”、“向下翻转”)
其他的
如果(内容<分数),则
文件:写入(分数)
文件:flush()
文件:close()
计时器。暂停(计时器1)
导演:变更场景(“菜单”、“向下翻转”)
其他的
文件:写入(内容)
文件:flush()
文件:close()
计时器。暂停(计时器1)
导演:变更场景(“菜单”、“向下翻转”)
结束
结束
结束
其他的
本地文件1=io.open(路径“w”)
局部瓦拉瓦尔=0
文件1:写入(walaVal)
文件1:close()
如果(stopscore==false),则
分数=分数+1
scoreText.text=“分数:。。分数
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x=0
scoreText.y=30
印刷品(目录)
结束
如果(stopscore==true),则
本地文件=io.open(路径“w”)
文件:写入(分数)
文件:flush()
文件:close()
计时器。暂停(计时器1)
导演:变更场景(“菜单”、“向下翻转”)
结束
结束
结束

内容返回nil,因为此代码出现问题
local file=io.open(path,“w”)
当您调用此选项时,它将删除文件的所有内容。要解决此问题,您必须删除模式“w”。当您调用类似此local
file=io.open(path)
的本地文件时,当您需要更新分数时,您应将模式设置为“w”为了进一步理解我所说的,我将编写并解释代码

--first check the file if exist
   local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
   local file = io.open(path)

-- if file exist check the content and read the score else create a file and write the score

  if file then
      local reader = io.open( path, "r" )
      local contents = reader:read("*n")
-- if content is less than myScore Update the Score
     if contents < myScore then
     file = io.open(path,"w")
     file:write(myScore)
         file:flush()
         file:close()
     end
 else
    file = io.open(path,"w")
    file:write(myScore)
    file:flush()
    file:close()
 end
--首先检查文件是否存在
本地路径=system.pathForFile(“myfile.txt”,system.documents目录)
本地文件=io.open(路径)
--如果文件存在,检查内容并读取分数,否则创建文件并写入分数
如果文件
本地读卡器=io.open(路径“r”)
本地内容=读卡器:读(“*n”)
--如果内容少于myScore,则更新分数
如果内容
希望我能给你解释清楚:)