Friday, 15 August 2014

file handling - Stuck with I/O operations in lua -



file handling - Stuck with I/O operations in lua -

i'm trying design game in lua (this first game since i'm new language) okay.. have run score counter adds +1 on screen tap. want want save high score permanently.

i want display high score ('30' in case) separately on screen. , i'm stuck. tried tweaking i/o library, made things go more complicated. can help me please?

this have tried:

local f1 = io.open ("scoredata.txt", "a") function gameover() disp_tempscore() if score>highscore -- here initial value of highscore 0 highscore=score io.write (highscore) end score=0 -- turns score 0. (there separate score counter counts score , stores in 'score') mainscreen() local f2 = io.open ("scoredata.txt", "r") if f2~= nil save_high = io.read(highscore) end text_display2= display.newtext("best : " .. highscore, 0, 0, "helvetica", 90) text_display2.x = centerx text_display2.y = centery + 80 text_display2.alpha=1

now here score 'best :' appears highest score, multiple runs @ same time. mean when start game in corona simulator , play game 5 times (suppose), highest score shows right data. but, when exit simulator , restart it, highest score vanishes , displays 0. how can store info (score in case) permanently?

edit: (this had tried lastly (one of hits tried))

local function disapp (event) -- event listener responds "tap" local obj = event.target display.remove (obj) audio.play(sound) transition.cancel (event.target.trans) score=score+1 curr_scoredisp = score scoretext.text=score local f1 = io.open (path, "a") --writing score file if score>highscore -- initial value of highscore 0 highscore=score f1:write(highscore) end io.close(f1)

then :

local function disp_permscore() local f1 = io.open (path, "r") f1:read(highscore) text_display2= display.newtext("best : " .. highscore, 0, 0, "helvetica", 90) text_display2.x = centerx text_display2.y = centery + 80 text_display2.alpha=1 io.close(f1) end

this function reads score file , displays it. now? help in rectifying problem in way?

i think issue never write score file:

io.write (highscore)

should

f1:write(highscore)

and should close file no longer need (will prevent loss of info due crash, e.g.). don't forget utilize system.pathforfile( filename, system.documentsdirectory) described in corona docs, file set in right location.

lua file-handling

No comments:

Post a Comment