1

私のアプリをAndroidデバイスにインストールして実行した後、ハイスコアをクリックすると、私の問題を初めて実行するアプリがこのコードである場合、「ハイスコア:0」が投稿されるはずです

local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

Androidデバイスにsystem.DocumentsDirectoryがないようです問題を書く前にテキストファイルの拳を作成する必要があります私のパスはmyfile.textを作成するために必要なので、system.DocumentsDirectoryの代わりになるものは何ですか? system.ResourceDirectory を使用できないため、唯一の読み取り可能であり、書き込み可能ではありません

これは、ユーザーがインストール後にゲームをプレイする前に最初にハイスコアをチェックする場合に使用される私のhighscore.lua用です

   local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )

   local file = io.open( path, "r" )

   local savedData = file:read( "*n" )

   if (savedData == nil) then
        file=io.open(path,"w")
        local newVal="0"
        file:write(newVal)
        file:flush()
        local scoreText = display.newText("score: " .. newVal, 0, 0, "BorisBlackBloxx", 50)
        scoreText:setReferencePoint(display.CenterLeftReferencePoint)
        scoreText.x = 0
        scoreText.y = 30
   else
        local scoreText = display.newText("score: " .. savedData, 0, 0, "BorisBlackBloxx", 50)
        scoreText:setReferencePoint(display.CenterLeftReferencePoint)
        scoreText.x = 0
        scoreText.y = 30
   end

これは私のgame.luaで、ユーザーが初めてゲームをプレイする場合に使用します

            local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
            local reader = io.open( path, "r" )                             
            local contents = reader:read("*n")
            local file = io.open( path, "w" )

                    if (contents == nil) then
                        local walaVal="0"
                        file:write(walaVal)
                        file:flush()
                    else
                        file:write(contents)
                        file:flush()
                    end
4

2 に答える 2

0

これらの関数を使用して、ファイルを非常に簡単に保存およびロードできます。私はいつも彼らと一緒に仕事をしていますが、まったく問題はありませんでした。

require( "json" )
    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


    -- The part related to your code :
    local scores = loadTable( "scores.json" )
    if scores == nil then
        -- First time in the game
        scores = {}
        scores.highScore = 0
        saveTable( scores, "scores.json" )
    end
于 2013-07-24T14:15:22.080 に答える