1

updateScore に問題があります。私の機能は、ユーザーが初めてゲームをプレイした場合です。

スコアを記録するために myFile.txt という名前のファイルを作成します。これを行うコードは、(リーダーの場合) ファイルが存在するかどうかを確認することです。ファイルが存在しない場合は、のファイルに移動します。次に、コンテンツにスコアの値が必要であり、それを使用して比較してハイスコアを取得できます。

問題は、私のコンテンツが常にnil値を返すため、プレイするときに常に得られるスコアが、私のハイスコアであるはずのスコアに置き換わることです。何が間違っているのかわかりません。

これが私のコードです

function updateScore()

    local path = system.pathForFile( "myfile.txt", system.DocumentsDirectory )
    local reader = io.open( path, "r" )
    local file = io.open( path, "w" )

    if reader then

        reader:close()
        local reader1 = io.open( path, "r" )
        local contents = reader1:read("*n")


        if (stopscore == false) then
            score = score + 1
            scoreText.text = "score: " .. score
            scoreText:setReferencePoint(display.CenterLeftReferencePoint)
            scoreText.x = 0
            scoreText.y = 30
        end

        if (stopscore == true) then

            if (contents == nil) then
                local file = io.open( path, "w" )
                file:write(score)
                file:flush()
                file:close()
                timer.pause(timer1)
                director:changeScene( "menu", "downFlip" )

            else

                if (contents < score) then
                    file:write(score)
                    file:flush()
                    file:close()
                    timer.pause(timer1)
                    director:changeScene( "menu", "downFlip" )
                else
                    file:write(contents)
                    file:flush()
                    file:close()
                    timer.pause(timer1)
                    director:changeScene( "menu", "downFlip" )
                end

            end
        end

    else

        local file1 = io.open( path, "w" )
        local walaVal=0
        file1:write(walaVal)
        file1:close()

        if (stopscore == false) then
            score = score + 1
            scoreText.text = "score: " .. score
            scoreText:setReferencePoint(display.CenterLeftReferencePoint)
            scoreText.x = 0
            scoreText.y = 30
            print(contents)
        end

        if (stopscore == true) then
            local file = io.open( path, "w" )
            file:write(score)
            file:flush()
            file:close()
            timer.pause(timer1)
            director:changeScene( "menu", "downFlip" )
        end

    end
end
4

1 に答える 1