Простой рекорд с использованием предпочтений (Lua - Corona SDK) - PullRequest
0 голосов
/ 29 апреля 2018

И заранее спасибо за помощь!

Я пытаюсь реализовать в моей игре функцию рекордов, используя «предпочтения» в 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 голосов
/ 29 апреля 2018

Попробуйте

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
...