-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.lua
61 lines (48 loc) · 1.46 KB
/
score.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local M = {}
M.score = 0
-- options
function M.init(options)
local custom_options = options or {}
local option = {}
option.fontSize = custom_options.fontSize or 40
option.font = custom_options.font or "Matura MT Script Capitals"
option.x = custom_options.x or 80
option.y = custom_options.y or 280
option.maxDigits = custom_options.maxDigits or 6
local prefix = ""
M.format = "%" .. prefix .. option.maxDigits .. "d"
M.scoreText = display.newText( string.format ( M.format, 0 ), option.x, option.y, option.font, option.fontSize );
return M.scoreText
end
-- update score
function M.set(value)
M.score = tonumber(value)
M.scoreText.text = string.format(M.format, M.score)
end
--[[ -- get current score
function M.get()
return M.score
end
-- add to score
function M.add(amount)
M.score = M.score + tonumber(amount)
M.scoreText.text = string.format(M.format, M.score)
end
-- save score
function M.save()
local saved = system.setPreferences( "app", {currentScore = M.score} )
print(currentScore)
if (saved == false) then
print("Error, could not save score.")
end
end
-- load score
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 no longer exist in storage)" )
end
end ]]
return M