对于刚开始接触 Corona/Lua 游戏开发的新手,经常会有这样一个问题
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 表,包含
fontSize — 显示分数文本的大小。font — 用于显示分数文本的字体。有关在 Corona 应用中使用自定义字体的详细信息,请参阅 使用自定义字体 指南。x — 绘制分数显示的 x 位置。y — 绘制分数显示的 y 位置。maxDigits — 得分的估计最大位数。leadingZeros — 根据是否需要前导零来设置 true 或 false。如果没有指定任何受支持选项,我们将回退到合理的默认值,在本例中,为 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
让我们快速检查一下这三个新函数
如果我们调用 set() 函数,分数显示对象 (M.scoreText) 在视觉上会更新为新的值。
get() 方法只是简单的返回当前得分供其他用途使用。
add() 函数允许我们将一个值添加到当前得分中并更新显示对象。虽然这个理念可以扩展为一个额外的“subtract”函数,但只将一个负值传递为 amount 更为有效。
我们的模块需要的最后一项功能是保存和加载得分的 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
这两个函数相对来说比较简单
save() 函数将当前得分 (M.score) 保存到应用程序的本地存储,其键名为 currentScore。如果这个进程失败,它将会在控制台中输出一个 error 消息。
load() 函数从应用程序的本地存储中加载关联的键,并将其设置为变量 score。如果这个进程成功,它将会返回该值,否则会在控制台中输出一个 error 消息。
在创建完我们的 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() 函数。这会创建文本显示,使之居中显示在屏幕顶部,并且将它设置为
从这一刻起,下面的动作都可以只使用一行代码执行
| 命令 | 结果 |
|---|---|
score.set( value ) |
将得分设置为 value。 |
local currentScore = score.get() |
获取当前得分。 |
score.add( value ) |
value。 |
score.save() |
将得分保存到本地存储。 |
local savedScore = score.load() |
加载先前保存的得分。 |
希望这篇教程展示了在 Corona 中对得分进行处理是多么简单,包括在设备上保存和加载得分以便在应用会话之间保持不变。