简单的高分使用偏好(Lua - Corona SDK)

时间:2018-04-29 15:35:59

标签: mobile lua corona preferences

并提前感谢您的帮助!

我正试图在我的游戏中使用Lua中的“偏好”来实现一个高分特色,但是我错过了一些东西,这是我的代码:

local highscore_letta = system.getPreference( "app", "highscore", "number" )

-- if preference do not exist, create it and set it to 0 (first time playing the game)
if (highscore_letta == nil) then
    local appPreferences =
    {
        highscore = "0"
    }
    system.setPreferences( "app", appPreferences )
end

-- if the score is higher than current highscore, update it with the new one
if (_G.player_score > highscore_letta) then
    local appPreferences =
    {
        highscore = _G.player_score
    }
    system.setPreferences( "app", appPreferences )
end

玩家第一次失败后,游戏崩溃说它正在比较“highscore_letta”中的空值。 经过第二次尝试,游戏不会崩溃,但它坚持0并且永远不会更新它。

有什么建议吗?我无法弄清楚我错过了什么。 再次感谢!

1 个答案:

答案 0 :(得分:0)

尝试

local highscore_letta = system.getPreference( "app", "highscore", "number" ) or 0

-- if the score is higher than current highscore, update it with the new one
if (_G.player_score > highscore_letta) then
    local appPreferences =
    {
        highscore = _G.player_score
    }
    system.setPreferences( "app", appPreferences )
end