【问题标题】:updateScore not working更新分数不起作用
【发布时间】:2013-07-28 22:55:29
【问题描述】:

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

它将创建我的名为 myFile.txt 的文件来记录分数,现在执行此操作的代码是(如果阅读器则)查看文件是否存在,如果不存在,它将转到我的 else 如果已经有一个文件,那么我的内容应该有一个分数的值,然后我可以用它来比较并得到我的高分。

问题是我的内容总是返回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

【问题讨论】:

    标签: android lua coronasdk


    【解决方案1】:

    内容返回 nil 因为此代码出现问题 local file = io.open( path, "w" ) 当你调用它时它会清除文件的所有内容来解决这个问题你必须删除模式“w”当你调用本地文件时像这样本地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
    

    希望我为你解释清楚:)

    【讨论】:

      猜你喜欢
      • 2012-07-26
      • 1970-01-01
      • 2016-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多