【问题标题】:Corona/json Using json to save highscoresCorona/json 使用json保存高分
【发布时间】:2013-12-04 02:27:46
【问题描述】:

我正在尝试创建一个使用 json 保存高分的游戏。但我遇到了麻烦。我的一个朋友帮我设置了 json。如果他们以前从未玩过或者如果他们已经击败了他们的高分,它应该保存高分,并将它在控制台上打印出来。但它似乎并没有保存分数,因为它一直返回 nil 并显示当前分数。本节的代码如下。提前致谢。

local json = 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 contents = ""
   local myTable = {}
   local file = io.open( path, "r" )
   if file then 
        local contents = file:read( "*a" )
        myTable = json.decode(contents);
        io.close( file )
   return myTable 
   end  
   return nil   
end

        myGameSettings = {}
        highscore = myGameSettings.highScore
        if(highscore == nil)then
        highscore = score
        myGameSettings.highScore = highscore
        print("score was nil")
        end
        if(highscore < score)then
        highscore = score
        myGameSettings.highScore = highscore
        print ("highscore beaten")
        end
        displayHighScoreNumber = display.newText("Your score: ", 160, 150,        
                    native.systemFontBold, 40 )
        displayHighScoreNumber.text = highscore
        displayingHighScore = 1;

        myGameSettings = loadTable("mygamesettings.json")

【问题讨论】:

    标签: json coronasdk


    【解决方案1】:

    我实际上有一些自己的 JSON 保存代码可供您使用。这是一个可用于操作系统时间的示例。要查看 txt 文件,请转到您的 Corona 沙箱(可通过 Corona 模拟器访问)。

    local json = require("json")
    time = os.time() --Get OS Time 
    print (time)
    local gameState = {
        time,
    }
    function Load( pathname ) --load a table from a file, this is just one way to do saves
        local data = nil
        local path = system.pathForFile( pathname..".txt", system.DocumentsDirectory  ) --get the file path
        local fileHandle = io.open( path, "r" ) -- open the file
        if fileHandle then -- if opening the file worked, read the file.
            data = json.decode( fileHandle:read( "*a" ) ) -- decode the JSON into a lua table
            io.close( fileHandle ) -- close the opened file
        end 
        return data -- return the loaded table
    end
    
    function Save( data, pathname ) -- save a table to a file
        local success = false 
        local path = system.pathForFile( pathname..".txt", system.DocumentsDirectory  ) -- get the file path
        local fileHandle = io.open( path, "w" ) -- open the file 
        if fileHandle then -- if it worked write the file
            fileHandle:write( json.encode( data ) ) -- encode the table into JSON, then write it to file
            io.close( fileHandle ) -- close the file
            success = true
        end
        return success --return true if it worked, false otherwise
    end
    
    Save( gameState, "time4" ) --Game state is the table of values, time 4 is the file name
    local gameState = Load( "time4" )
    --Now if you print out gamestate, it should print the time.
    

    【讨论】:

      【解决方案2】:
          myGameSettings = {}
          myGameSettings = loadTable("mygamesettings.json")
      
          highscore = myGameSettings.highScore
          if(highscore == nil)then
              highscore = score
              myGameSettings.highScore = highscore
              print("score was nil")
          end
          if(highscore < score)then
              highscore = score
              myGameSettings.highScore = highscore
              print ("highscore beaten")
          end
          displayHighScoreNumber = display.newText("Your score: ", 160, 150,        
                      native.systemFontBold, 40 )
          displayHighScoreNumber.text = highscore
          displayingHighScore = 1;
      

      您正在创建一个空表,它的 highScore 总是为零。尝试先加载表格。更新后不要忘记保存表格。

      【讨论】:

        猜你喜欢
        • 2013-07-20
        • 1970-01-01
        • 2015-09-18
        • 2017-09-27
        • 2015-04-08
        • 1970-01-01
        • 2011-12-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多