2017-05-06 32 views
0

我在碰撞屬性中的比分和高分在我的比賽中,所以每當我的對象在某個邏輯分數上碰撞並且高分增加,因爲我沒有在我的遊戲中死亡舞臺,所以當我重新啓動我的遊戲高分從零開始我希​​望高分應該從以前的高分開始,只有在得分>高分時才更新。我寫的代碼,但是當我從物體碰撞6開始高分懇請這樣告訴我的方式來解決它如何更新高分

local Highscore = 0 
    score = 0 
    local function updateText() 
     HighscoreText.text = "Highscore: " .. Highscore 
    end 

-- collision Property -- 

local function onCollision(event) 
    if (event.phase == "began") then 
-- logic -- 

    -- score update-- 

-- highscore update-- 
if score > Highscore then 
    Highscore = score 
end 
updateText() 


end 
end 
end 

Runtime:addEventListener("collision" , onCollision) 

end 
+0

使用[Simple-Table-Load-Save-Functions-for-Corona-SDK](https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK) - 兩個非常簡單的加載和保存功能來存儲Lua表並將其讀回。需要Corona SDK JSON庫。 – ldurniat

回答

1

我創建使用處理持久性的高分小模塊它不工作Corona SDK JSON庫。

-- File name 
local fileName = "highscore.json" 
-- Path for File 
-- The system.DocumentsDirectory will be backed up by synchronization in iOS 
local pathForFile = system.pathForFile(fileName, system.DocumentsDirectory) 

-- JSON library included in Corona SDK 
local json = require("json") 

-- This table holds two functions 
-- persistHighscore, to save the highscore to file 
-- fetchHighscore, to retrieve the saved highscore 
local persistence = {} 

-- Persists the highscore table 
-- It receives only one parameter which must be a table 
-- containing the highscore in a Key, Value pair 
-- Example: {highscore = 10} 
persistence.persistHighscore = function(highscoreTable) 
    local encoded = json.encode(highscoreTable) 
    local file, errorString = io.open(pathForFile, "w") 

    if not file then 
     print("Opening file failed: " .. errorString) 
    else 
     file:write(encoded) 
     file:flush() 
     file:close() 
    end 
end 

-- Returns the Highscore table 
-- If there is a highscore file it will be read and a 
--table containing the highscore will be returned 
persistence.fetchHighscore = function() 
    local decoded, pos, msg = json.decodeFile(pathForFile) 
    if not decoded then 
     print("Decode failed at "..tostring(pos)..": "..tostring(msg)) 
    else 
     print("File successfully decoded!") 
     return decoded 
    end 
end 

return persistence 

在項目:

local persistence = require("highscorePeristence") 

如果你不熟悉科羅娜SDK模塊加載和Lua一定要 檢查: Corona SDK API Doc - require 在您的高分數處理邏輯,當你發現分數高於目前的高分:

if score > highscore then 
    -- Container table to be stored in JSON 
    local scoreTable = { 
     highscore=score 
     } 
    persistence.persistHighscore(scoreTable) 
end 

而且wh您正在加載項目並且想要加載存儲的高分:

local highscore 
local scoreTable = persistence.fetchHighscore() 
-- No highscore was stored 
if not scoreTable then 
    -- Initialize the highscore with 0 since there was no other value before 
    highscore = 0 
else 
    -- Initialize the highscore with the value read from the JSON decoded table 
    highscore = scoreTable.highscore 
end