2012-12-12 3 views
0

안녕하세요 저는 대화식 전자 책인 corona sdk를 사용하여 응용 프로그램을 작성하고 있습니다. 내가 할 수 있어야 할 일은 현재 변수를 저장하고 사용자가 이미 가지고있는 장면은 물론 선택 사항을 포함하는 시작시 외부 파일을로드하는 것입니다. 이 모든 것을 전역 변수로 저장합니다. 각 선택 항목에는 0 또는 1 값이 지정되고 장면 이름은 지정된 숫자 즉 배열로 저장됩니다. 장면 1 = 배열에서 1. 어떤 도움이라도 믿을 수 없을 정도로 감사 할 것입니다. 미리 감사드립니다.json을 사용하여 코로나 sdk 상태 저장

답변

0

저장하려는 변수가있는 테이블을 만들고이 두 함수를 사용하여 파일에 테이블을 저장하고 파일을 읽고 저장된 테이블을 반환하십시오.

local function write(table, fileName) 
    local filePath = system.pathForFile(fileName, system.DocumentsDirectory) 
    local file = io.open(filePath, "w") 
    local encodedTable = json.encode(table) 
    file:write(encodedTable) 
    io.close(file) 
end 

local function read(fileName) 
    local filePath = system.pathForFile(fileName, system.DocumentsDirectory) 
    local file = io.open(filePath, "r") 

    if file then 
     local contents = file:read("*all") 
     local decodedTable = json.decode(contents) 

     io.close(file) 
     return decodedTable 
    else 
     return false 
    end 
end