score in crawl space Library

How to save and load different HighScores for different Levels using crawl space Library?Please help me..

I have not tried the crawl space library, but Ice does the same thing.

Link: http://developer.anscamobile.com/code/ice

I am currently using director class so try making a main.lua,Ice2.lua and a Ice3.lua. Try using each snippet of code in the right module. I have tested this out and it work, so try. If you need further explanation check out the link above.

Here is some sample code:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
-- main.lua
 
 
function changeScene (e)
        if(e.phase == "ended") then
                director:changeScene(e.target.scene)
        end
end
 
local director = require("director");
local mainGroup = display.newGroup();
 
mainGroup:insert(director.directorView);
director:changeScene("ice2");
 
-----------------------------------------------------------------------------
 
--Ice2.lua:
 
module(..., package.seeall)
 
function new()
 
        local ice2Group = display.newGroup();
 
require( "ice" )
 
local scores = ice:loadBox( "scores" )
 
local high = scores:retrieve("best")
print(high)
 
local gameOver = false
 
local physics = require("physics")
physics.start()
physics.setGravity(0,5)
local ballGroup = display.newGroup()
local score = 0
 
local score_txt =display.newText("Score: ",0,0,nil,30)
score_txt.x = 100; score_txt.y = 50
score_txt:setTextColor(255,0,0)
score_txt.text = "Score: ".. score
 
 
point = 0
local scoreText = display.newText("Points: ",0,0,nil,30)
scoreText.x = 100
scoreText.y = 100
scoreText:setTextColor(255,0,0)
scoreText.text = "Points:".. point
 
local function spawn_ball()
if gameOver == false then
local ball = display.newCircle(ballGroup,0,0,30)
ball.x = math.random(50,300)
ball.y = -50
physics.addBody(ball, {radius = 30})
ball.name = "ball"
end
end
 
timer.performWithDelay(500, spawn_ball, 0)
 
 
local basket = display.newRect(0,0,50,70)
basket.x = display.contentWidth/2
basket.y = display.contentHeight - 80
physics.addBody(basket, "static")
 
local function drag(event)
        if event.phase == "began" then
                event.target.isFocus = true
        elseif event.phase == "moved" and event.target.isFocus then
                event.target.x = event.x
                event.target.y = event.y
        end
return true
end
 
basket:addEventListener("touch", drag)
 
 
local function show_highScore()
gameOver = not gameOver
 
        scores:storeIfHigher( "best", score )
scores:save()
        local highText = display.newText("", 0,0,nil,30)
        highText.x = display.contentWidth/2
        highText.y = display.contentHeight/2
        if score > high then
        highText.text = "HighScore: ".. scores:retrieve("best")
        scoreText.text = "HighScore: ".. scores:retrieve("best")
        else
        highText.text = "NO NEW HIGH SCORE!"
        end
end
 
 
local function show_text()
        local text = display.newText("+1", 0,0, nil, 30)
        
        score = score + 1
        
        if score > scores:retrieve("best") then
                print("new high score")
        end
        
        score_txt.text = "Score: ".. score
        text.alpha = 1
        text.x = basket.x
        text.y = basket.y - 100
        transition.to(text, {time=1000,y = text.y - 50, alpha = 1, onComplete = function() display.remove(text) end})
end
 
local function onCollision(event)
        if event.phase == "began" and event.other.name == "ball" then
                display.remove(event.other)
                show_text()
        end
end
 
basket:addEventListener("collision", onCollision)
 
timer.performWithDelay(50000, show_highScore, 1)
 
 
Tap = display.newText( "Tap", 20, 200, "Helvetica", 20 )
Tap.x = 30
Tap.y = 450
 
function ther5(event)
director:changeScene( "Ice3", "fade" )
end
Tap:addEventListener("tap", ther5);
 
        return ice2Group;
 
end
 
-------------------------------------------------------------------------------------
 
 
-- Ice3.lua
 
module(..., package.seeall)
 
function new()
 
        local ice2Group = display.newGroup();
-- main.lua
require("ice")
myData = nil
 
-- load previously saved data
-- EDIT:  if no data has been saved, it will create the myData.ice where data will be saved
local scores = ice:loadBox( "scores" )
 
local score = scores:retrieve( "best" )
 local scoreText = display.newText( "Score: "..score, 20, 0,nil, 20)
 scoreText:setTextColor(255,255,255)
 
local function show_highScore()
        local highText = display.newText("", 0,0,nil,30)
        highText.x = display.contentWidth/2
        highText.y = display.contentHeight/2
        if score > 40 then
        highText.text = "HighScore: ".. scores:retrieve("best")
        else
        highText.text = "NO NEW HIGH SCORE!"
        end
end
 
timer.performWithDelay(2000, show_highScore)
 
 
 
Tap = display.newText( "Tap", 20, 170, "Helvetica", 30 )
Tap.x = 100
Tap.y = 400
 
function ther5(event)
director:changeScene( "ice2", "fade" )
end
Tap:addEventListener("tap", ther5);
 
        return ice2Group;
 
end
views:4445 update:2011/12/27 8:54:37
corona forums © 2003-2011