hi,
I just satrted this app to learn JSON and I cant get it to work.
heres the code :
local affichageNiveau local numNiveau = 1 local W\_ = display.contentWidth /2 local H\_ = display.contentHeight /2 local nombreDeNiveau = 300 --code function main() local numNiveauFilename = "numniveau.data" numNiveau = loadValue(numNiveauFilename) numNiveau = tonumber(numNiveau) for i = 1, nombreDeNiveau do if numNiveau ~= i then print("in line 43") numNiveau = 1 saveValue(numNiveauFilename, tostring(numNiveau)) break end end addLevelScreen() end function addLevelScreen() affichageNiveau = display.newText(tostring(numNiveau), W\_, H\_, native.systemFont, 56) affichageNiveau:setFillColor(1,0,1) affichageNiveau:addEventListener("tap", addValue) end function addValue() print("addValue") print (numNiveau) numNiveau = tonumber(numNiveau) numNiveau = numNiveau + 1 affichageNiveau:removeEventListener("tap", addValue) affichageNiveau:removeSelf() addLevelScreen() local numNiveauFilename = "numniveau.data" saveValue(numNiveauFilename, tostring(numNiveau)) end --saving function using json function saveValue(strFilename, strValue) print("in saveValue") local theFile = strFilename local theValue = strValue local path = system.pathForFile( theFile, system.DocumentsDirectory ) local file = io.open( path, "w+" ) if file then -- If the file exists, then continue. Another way to read this line is 'if file == true then'. file:write(theValue) -- This line will write the contents of the table to the .json file. io.close(file) -- After we are done with the file, we close the file. return true -- If everything was successful, then return true end end --loading function using json -- Load specified encrypted file, or create new file if it does not exist function loadValue(strFilename) print("in loadValue") local theFile = strFilename local path = system.pathForFile( theFile, system.DocumentsDirectory ) local file = io.open( path, "r" ) if file then -- If file exists, continue. Another way to read this line is 'if file == true then'. local contents = file:read( "\*a" ) -- read all contents of file into a string io.close( file ) -- Since we are done with the file, close it. return contents -- Return the table with the JSON contents else return '' -- Return nothing end end main()
please help