How to save after object remove then when relaunch the object will not display again ?
main.lua
local loadsave = require("loadsave") myTable = loadsave.loadTable("Table.json") if myTable ==nil then myTable = {} loadsave.saveTable(myTable, "Table.json") else end local arr = {} function onTouch(e) if e.phase == "began" then e.target:removeSelf() loadsave.saveTable(myTable, "Table.json") end end for i = 0 ,4 do arr[i] ={} for j = 0,4 do arr[i][j] = display.newRect(0,0,40,40) arr[i][j].x = i\*44+60 arr[i][j].y = j\*44+100 arr[i][j]:addEventListener("touch",onTouch) end end
loadsave.lua
local \_ = {} local json = require("json") local DefaultLocation = system.DocumentsDirectory local RealDefaultLocation = DefaultLocation local ValidLocations = { [system.DocumentsDirectory] = true, [system.CachesDirectory] = true, [system.TemporaryDirectory] = true } function \_.saveTable(t, filename, location) if location and (not ValidLocations[location]) then error("Attempted to save a table to an invalid location", 2) elseif not location then location = DefaultLocation end local path = system.pathForFile( filename, location) local file = io.open(path, "w") if file then local contents = json.encode(t) file:write( contents ) io.close( file ) return true else return false end end function \_.loadTable(filename, location) if location and (not ValidLocations[location]) then error("Attempted to load a table from an invalid location", 2) elseif not location then location = DefaultLocation end local path = system.pathForFile( filename, location) local contents = "" local myTable = {} local file = io.open( path, "r" ) if file then -- read all contents of file into a string local contents = file:read( "\*a" ) myTable = json.decode(contents); io.close( file ) return myTable end return nil end function \_.changeDefault(location) if location and (not location) then error("Attempted to change the default location to an invalid location", 2) elseif not location then location = RealDefaultLocation end DefaultLocation = location return true end return \_