Corona / json使用json保存高分

时间:2013-12-04 02:27:47

标签: json corona

我正在尝试创建一个使用json保存高分的游戏。但我遇到了麻烦。我的一个朋友帮助我建立了json。如果他们之前从未玩过,或者如果他们击败了他们的高分,那么应该保存高分,并打印出它在控制台上做的事情。但它似乎没有保存得分,因为它一直返回零并显示当前得分。本节的代码如下。提前谢谢。

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")

2 个答案:

答案 0 :(得分:0)

我实际上有一些我自己可以使用的JSON保存代码。以下是可用于OS Time的示例。要查看txt文件,请转到Corona Sandbox(可通过Corona Simulator访问)。

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.

答案 1 :(得分:0)

    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;

您正在创建一个空表,其总是具有零的高分数。首先尝试加载表格。更新后不要忘记保存表格。