使用偏好设置实现简单的高分 (Lua - Corona SDK)

感谢您的帮助!

我正在尝试在我的游戏中使用 Lua 的“偏好设置”实现高分功能,但我还是缺少了些什么,以下是我的代码:

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

-- 如果偏好设置不存在,则创建并将其设置为0(第一次玩游戏)
if (highscore_letta == nil) then
    local appPreferences =
    {
        highscore = "0"
    }
    system.setPreferences( "app", appPreferences )
end

-- 如果分数高于当前高分,则使用新分数更新高分
if (_G.player_score > highscore_letta) then
    local appPreferences =
    {
        highscore = _G.player_score
    }
    system.setPreferences( "app", appPreferences )
end

在玩家第一次输掉游戏后,游戏会崩溃,并显示比较“highscore_letta”中的空值。 第二次尝试后,游戏不会崩溃,但仍然保持为0,从未更新过。

有什么建议吗?我无法想出我缺少了什么。 再次感谢!

点赞
用户7026995
用户7026995

尝试

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

-- 如果分数比当前的最高分高,就用新分数更新它
if (_G.player_score > highscore_letta) then
    local appPreferences =
    {
        highscore = _G.player_score
    }
    system.setPreferences( "app", appPreferences )
end
2018-04-29 20:55:55