0

コロナSDKの新機能で、シミュレーターでファイル(ゲームデータを保存する)をロードして保存する方法を見つけようとしています。(実際のデバイスでデバッグする必要がなく、毎回変数の変更を確認するのに15秒かかりません)。

ここのチュートリアルに従いました: http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/ そして、この問題に既に対処しているスタックオーバーフローには何も見つかりませんでした.

現在、ファイルの読み取りと保存のための次のコードがあります。

local readJSONFile = function( filename, base )

    -- set default base dir if none specified
    if not base then base = system.ResourceDirectory; end

    -- create a file path for corona i/o
    local path = system.pathForFile( filename, base )

    -- will hold contents of file
    local contents

    -- io.open opens a file at path. returns nil if no file found
    local file = io.open( path, "r" )
    if file then
       -- read all contents of file into a string
       contents = file:read( "*a" )
       io.close( file ) -- close the file after using it
    end

    return contents
end

local writeToFile = function( filename, content )
    -- set default base dir if none specified
    if not base then base = system.ResourceDirectory; end

    -- create a file path for corona i/o
    local path = system.pathForFile( filename, base )

    -- io.open opens a file at path. returns nil if no file found
    local file = io.open( path, "w" )
    if file then
       -- write all contents of file into a string
       file:write( content )
       io.close( file ) -- close the file after using it
    end
end

JSONファイルを読み取り、別のデータで保存し、ロードすると、それが持続するように見えるので、うまくいくようです。ただし、IDE を閉じるとすぐに変更が失われます。さらに、私のシステム (mac book pro) 上の実際のファイルは変更されていません。

私が行った場合:

local json = require "json"
local wordsData = json.decode( readJSONFile( "trivia.txt" ) )
wordsData.someKey = "something different"
writeToFile("trivia.txt", json.encode( wordsData ) )  -- this only works temporarily

私のtrivia.txtファイルと同じディレクトリにあるファイルを読んでいてmain.lua、何かを変更してロードしようとしています。trivia.txtただし、上記のコードは、私の mac book pro に実際の変更を加えることはありません 。

これを行う適切な方法は何ですか?? ゲームの設定とゲーム データを保存する必要があります (これは雑学アプリです。最大 50 語とユーザーが選んだ答えを保存する必要があります)。IDE を閉じたときにファイルに書き込んだ内容が記憶されるように、データを保存する必要があります。

私の推測では、私が自分trivia.txtの をロードすると、IDE をロードするたびに、実際にはそのファイルの mac book pro を調べていると思います。しかし、シミュレーターで初めて実行すると、trivia.txt一時フォルダーに新しいフォルダーが作成されます(これがどこにあるのかわかりません)。同じコードを再実行すると、そこから読み取りが開始されます。右?

どんな助けでも大歓迎です!!! 私はコロナSDKが初めてなので、より詳細な回答に賛成票を投じてください

4

1 に答える 1

5

パスには system.DocumentsDirectory を使用することをお勧めします。最初にリソース ディレクトリから読み取り、次に DocumentsDirectory に格納できます。その後、いつでも DocumentsDirectory を探すことができます。これで問題は解決します。ここでは、ファイルが存在するかどうかを確認できるいくつかの関数を示します。もちろん、パスを変更できます

function saveTable(t, filename)
    local path = system.pathForFile( filename, system.DocumentsDirectory)
    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)
    local path = system.pathForFile( filename, system.DocumentsDirectory)
    local myTable = {}
    local file = io.open( path, "r" )
    local contents = ""
    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
于 2013-05-20T06:39:14.720 に答える