I used some code from some place on corona’s website but can’t find it again. I got it to work with my game but the high score only goes to 9 while the players score (hole_count) goes to double and triple digits. Would somebody please tell me what is going wrong. I don’t understand every line in the code
but I did get it to work but only up to 9. I am guessing that it has something to do with single and double digits but the research I have done hasn’t uncovered anything obvious.
Thanks for any help I do appreciate it.
local function save( event ) – function to save score
local path = system.pathForFile( “score.txt”, system.DocumentsDirectory ) – where the file will be saved
local file = io.open( path, “w+b” ) – opening the file
file:write(score …"") – writing the variable ‘score’ in the score.txt file
io.close( file ) – closing the file
– Saves our data
end
local function resumeStart()
local path = system.pathForFile( “score.txt”, system.DocumentsDirectory ) – where the file will be saved
local file = io.open( path, “r” ) – opens the file under the variable file
if file then – if there is a file then
local contents = file:read( “*a” ) – read the contents of the file, and read the whole string(*a)
local prevState = explode(", ", contents) – put the contents of score.txt into a table
print(‘file’)
score = prevState[1] – read the table at position 1 and assign the variable score
io.close( file ) – close file
else – if there is no file
score=0 – the score then starts at 0
end
end
–ANSCA sample code–
function explode(div,str)
if (div==’’) then return false end
local pos,arr = 0,{}
– for each divider found
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-3)) – Attach chars left of current divider
pos = sp + 1 – Jump past current divider
end
table.insert(arr,string.sub(str,pos)) – Attach chars right of last divider
return arr
end
–ANSCA sample code^–
– call the starting function
resumeStart()
local scoretext = display.newText(‘High Score:’,(_W*.3),(_H-30),native.systemFont,20)
local t = display.newText(score,(_W*.5),(_H-30),native.systemFont,20) – score text
t.text=score – update t to the score
local function scoretapped() – function called when user presses the add button
–score = score + 1 – increases the score by 50
– save() – calls save, so when the user reloads the application the score is still there
–hole_count.text = t.text
if (hole_count.text > score) then
score = hole_count.text
– score = hole_count.text
t.text = score – updates t to reflect new score
save( “score.txt”, score )
end
end [import]uid: 17539 topic_id: 13756 reply_id: 313756[/import]
[import]uid: 52491 topic_id: 13756 reply_id: 50704[/import]
[import]uid: 52491 topic_id: 13756 reply_id: 50871[/import]