I am having trouble with some code for save and loading a high score. It saves the score and loads a score but it will NOT count past 9. I think it is a problem with having a single digit or a double digit number. I can change the score in the system.DocumentsDirectory and it will show in the high score
but will change when the players score is > then 1. I mean the high score will change to 2. For example I type in 13 for the high score and score 2 points playing the game and the high score will
change to 2. If the high score is <= 9 than the high score seems to work but only up to 9.
So is there something in the code that is preventing the numbers switching to double digits.
I think this code is from Peach Pellen
I would greatly appreciate any help.
[lua]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( “*n” ) – 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
return contents
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-0 ) – 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-45),“Helvetica”,20*2)
scoretext.xScale=0.5;scoretext.yScale=0.5;
local t = display.newText(score ,_W*.60,_H-45,“Helvetica”,20*2) – score text
t.xScale=0.5; t.yScale=0.5;
local function scoretapped() – function called when user presses the add button
if (hole_count.text > score) then
score = hole_count.text
save( “score.txt”, score )
t.text = score – updates t to reflect new score
end
end[/lua] [import]uid: 17539 topic_id: 13806 reply_id: 313806[/import]