– Add Score
local points = -20
scoreText = display.newText( " " … points,288, 225, Font, 20)
scoreText:rotate(90)
localGroup:insert(scoreText)
– Add Collision Event
squirrel.myName = “squirrel”
acorn.myName = “acorn”
– Collision detection! Explained below in detail
squirrel:addEventListener(“collision”, squirrel)
function squirrel:collision (event)
if event.other.myName == “acorn” then
points = points + 10
scoreText.text = "Score: " … points
local crack = media.newEventSound (“chop.wav”)
media.playEventSound (crack)
end
end
– Highscore
local function save( event ) – function to save score
local path = system.pathForFile( “Highscore.txt”, system.DocumentsDirectory ) – where the file will be saved
local file = io.open( path, “w+b” ) – opening the file
file:write(Highscore …"") – 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( “Highscore.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’)
Highscore = prevState[1] – read the table at position 1 and assign the variable score
io.close( file ) – close file
else – if there is no file
Highscore=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-1)) – 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^–
resumeStart() – call the starting function
local scoretext = display.newText(‘Highscore:’,258,60,native.systemFont,20)
scoretext:rotate(90)
local t = display.newText(’’,258,80,native.systemFont,20) – score text
t.text=Highscore – update t to the score
save() – calls save, so when the user reloads the application the score is still there
t.text=Highscore – updates t to reflect new score
–This is what I have so far but it is not working, it links the scores and chooses the highest one but I have not found a way to link my displayed score to the highscores list, I think i can get it if I just change some values
do you know how to do this?
[import]uid: 71568 topic_id: 12541 reply_id: 49051[/import]