显示和保存分数

对于刚开始接触 Corona/Lua 游戏开发的新手,经常会有这样一个问题“我怎样才能保持分数?”本教程将详细介绍整个过程,内容包括

  1. 设置一个变量来保存分数。
  2. 显示分数。
  3. 保存(和检索)分数供以后使用。

分数模块

Corona 没有内置“分数”模块,因此我们构建一个模块,稍后你可以将其添加到你的应用中,首先从一个基本的初始化函数开始。

首先,在你的项目目录中创建一个新的空 Lua 文件。将此文件命名为 score.lua。在文本编辑器中打开此文件,并插入以下代码

local M = {}

M.score = 0  -- Set the score to 0 initially

function M.init( options )

    local customOptions = options or {}
    local opt = {}
    opt.fontSize = customOptions.fontSize or 24
    opt.font = customOptions.font or native.systemFont
    opt.x = customOptions.x or display.contentCenterX
    opt.y = customOptions.y or opt.fontSize*0.5
    opt.maxDigits = customOptions.maxDigits or 6
    opt.leadingZeros = customOptions.leadingZeros or false

    local prefix = ""
    if ( opt.leadingZeros ) then 
        prefix = "0"
    end
    M.format = "%" .. prefix .. opt.maxDigits .. "d"

    -- Create the score display object
    M.scoreText = display.newText( string.format( M.format, 0 ), opt.x, opt.y, opt.font, opt.fontSize )

    return M.scoreText
end

return M

在此,我们首先设置一个基本模块M此表将包含我们创建的所有函数,以便可以从包含此模块的任何模块调用这些函数。然后,我们为 M 表设置一个 score 属性,并将其最初设置为 0。对于 init() 函数,我们接受一个参数 options,该参数是一个 Lua 表,包含键值对。受支持的选项包括

如果没有指定任何受支持选项,我们将回退到合理的默认值,在本例中,为 24 点大小的默认系统字体,居中显示在屏幕顶部,最大为 6 位数字。

注意
  • 我们可以添加对齐属性和其他设置来对它进行扩展。例如,我们可以添加选项来控制左对齐右对齐分数文本。

  • 分数文本使用 string.format() API 来设置数字格式。在此,字符串将根据设置加上空格前缀或零前缀。此格式设置也会保存到模块 (M.format),供其他函数使用。有关字符串设置的更多详细信息,请参阅 格式化字符串值 教程。

设置、获取和添加函数

接下来,我们纳入一些用于设定获取将分数加到现有分数上的函数

function M.set( value )

    M.score = tonumber(value)
    M.scoreText.text = string.format( M.format, M.score )
end

function M.get()

    return M.score
end

function M.add( amount )

    M.score = M.score + tonumber(amount)
    M.scoreText.text = string.format( M.format, M.score )
end

return M

让我们快速检查一下这三个新函数

保存和加载分数

我们的模块需要的最后一项功能是保存和加载得分的 ability。如果得分一定要作为一个高分保存,那么此项功能就至关重要,此时它必须在应用程序会话之间保持不变

function M.save()

    local saved = system.setPreferences( "app", { currentScore=M.score } )
    if ( saved == false ) then
        print( "ERROR: could not save score" )
    end
end

function M.load()

    local score = system.getPreference( "app", "currentScore", "number" )

    if ( score ) then
        return tonumber(score)
    else
        print( "ERROR: could not load score (score may not exist in storage)" )
    end
end

return M

这两个函数相对来说比较简单

使用模块

在创建完我们的 score.lua 模块并编写完基本函数后,我们现在可以在应用程序中使用它了。在另一个场景或 Lua 模块中,例如,可以像这样实现评分模块

local score = require( "score" )

local scoreText = score.init(
{
    fontSize = 20,
    font = "CoolCustomFont.ttf",
    x = display.contentCenterX,
    y = 30,
    maxDigits = 7,
    leadingZeros = true
})

第一行是必须的 require() 行,它将 score.lua 模块包含进这个项目中,并且提供对我们上面编写的全部函数的 access。第二个块使用一系列基本参数调用模块的 init() 函数。这会创建文本显示,使之居中显示在屏幕顶部,并且将它设置为一个 zero-filled 7-digit得分。

从这一刻起,下面的动作都可以只使用一行代码执行

命令 结果
score.set( value ) 将得分设置为 value
local currentScore = score.get() 获取当前得分。
score.add( value ) 向当前得分添加 value。  
score.save() 将得分保存到本地存储。
local savedScore = score.load() 加载先前保存的得分。

结论

希望这篇教程展示了在 Corona 中对得分进行处理是多么简单,包括在设备上保存和加载得分以便在应用会话之间保持不变。