使用Corona SDK将值保存到Lua中的另一个文档

使用Corona SDK将值保存到Lua中的另一个文档,lua,global-variables,coronasdk,lua-table,Lua,Global Variables,Coronasdk,Lua Table,每当调用gameOver函数时,我都试图将高分保存到表中 当我返回应用程序并尝试读取高分时,它们显示为新文本,其中文本设置为该级别的正确高分 但它没有做它应该做的。我可以读取高分级别,但当我尝试更改表中的值时,高分文本不会更改 我有一个main.lua和一个myData.lua-高分表应该放在myData.lua中,以便从游戏中的各个级别达到它。 这是我的桌子 highScores = { 1, 2, 3, 4, 5, 6, 7 }

每当调用gameOver函数时,我都试图将高分保存到表中

当我返回应用程序并尝试读取高分时,它们显示为新文本,其中文本设置为该级别的正确高分

但它没有做它应该做的。我可以读取高分级别,但当我尝试更改表中的值时,高分文本不会更改

我有一个main.lua和一个myData.lua-高分表应该放在myData.lua中,以便从游戏中的各个级别达到它。 这是我的桌子

highScores = {  
    1,
    2,
    3,
    4,
    5,
    6,
    7
}
我试图用

highScores[1] = score
其中score是游戏中的分数计数

我意识到这不是解决问题的方法,我在谷歌上找到的东西对于我认为简单的任务来说似乎过于复杂了

我做错了什么

这是我的整个level1.lua-正在运行并试图将其分数保存到highScore表level1.lua的实际级别:

local composer = require( "composer" )
local scene = composer.newScene()
local myData = require( "myData" )
local physics = require("physics")
physics.setDrawMode( "hybrid" )
-- forward references
local w = display.actualContentWidth
local h = display.actualContentHeight
local dropCount = 0
local spawnShit = 0
local allowDrop = 1
local spawnTime = 17
local countdownTimer
local score
local gX = 0
local gY = 0
score = 0
local countDownNumber = 10
local gameOver

local scoreT = display.newText( {text="Score: "..score, font=system.nativeSystemFont, fontSize=14,} )
scoreT.x = w * 0.5
scoreT.y = h * 0.1

local countDownText = display.newText( {text="", font=system.nativeSystemFont, fontSize=14} )
countDownText.x = w * 0.5
countDownText.y = h * 0.2

local drop01 = display.newImage("drop01.png")
drop01.x = -100

local drop02 = display.newImage("drop02.png")
drop02.x = -100

local drop03 = display.newImage("drop03.png")
drop03.x = -100

local drop04 = display.newImage("drop04.png")
drop04.x = -100

local timerSpawn
local timer2

-- Display objects
local background = display.newImage( "bluebg.png" )
background.x = w*0.5
background.y = h*0.5
background.width = w 
background.height = h 

local bckBtn = display.newText({text="<--BACK", font=system.nativeSystemFont, fontSize=14})
bckBtn.x = 50
bckBtn.y = 20


local egon = display.newImage( "Egon.png" )
egon.x = w*0.5
egon.y = h*0.85
egon.width = 100
egon.height = 97

local destroyAll = display.newRect( 0, h, w, 10 )
destroyAll.width = w*2
destroyAll.alpha = 0

local overlayBg = display.newRect( -500, -500, w, h )
overlayBg:setFillColor( 0, 0, 0 )
overlayBg.alpha = 0.4

--functions

function gameOver ()
    if timerSpawn == nil then
    else
        timer.cancel(timerSpawn)
        timerSpawn = nil
        spawnShit = 0
    end
    if countdownTimer == nil then
    else
        timer.cancel(countdownTimer)
        countdownTimer = nil
    end
    highScores[1] = score
    transition.to( overlayBg, {x=w/2, y=h/2, time=500 } )
end

function goBack (event)
    if "began" == event.phase then
        gameOver()
        if timerSpawn == nil then
        else
            timer.cancel(timerSpawn)
        end
        if countdownTimer == nil then
        else
            timer.cancel(countdownTimer)
        end

    elseif event.phase == "ended" then 

        timer2 = timer.performWithDelay(1000, function()
            composer.gotoScene("select", "fade", 500)
        end)

        if overlayBg == nil then
        else
            overlayBg:removeSelf( )
        end

        return true
    end

    return true 
end

function moveEgon (event)
    if "moved" == event.phase then
        egon.x = event.x
    end

end 

------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
function spawnObjects (event)

    dropCount = math.random(1,4)

    --if stopTimer == 1 then
      --  timerSpawn = nil
        --spawnShit = nil
    --end
    if spawnShit == 1 then
        print( 'spawnShit' )
        if dropCount == 1 then
            -- Drop01 function and settings
            drop01 = display.newImage( "drop01.png" )
            drop01.x = math.random(10, 470)
            drop01.y = -40
            drop01.width = 50
            drop01.height = 50
            drop01.myName = "01"
            physics.addBody( drop01, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )

        elseif dropCount == 2 then 
            --Do shit for drop02
            drop02 = display.newImage( "drop02.png" )
            drop02.x = math.random(10, 470)
            drop02.y = -40
            drop02.width = 50
            drop02.height = 50
            drop02.myName = "02"
            physics.addBody( drop02, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )

        elseif dropCount == 3 then
            drop03 = display.newImage( "drop03.png" )
            drop03.x = math.random(10, 470)
            drop03.y = -40
            drop03.width = 50
            drop03.height = 50
            drop03.myName = "03"
            physics.addBody( drop03, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )

        elseif dropCount == 4 then
            drop04 = display.newImage( "drop04.png" )
            drop04.x = math.random(10, 470)
            drop04.y = -40
            drop04.width = 50
            drop04.height = 50
            drop04.myName = "04"
            physics.addBody( drop04, "dynamic", {density=0.9, friction=0.1, bounce=0.8 } )
        end
    end

    return true
end

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------

function onCollision (event)

    if "began" == event.phase then
    --v--do shit when touching surface

        if event.other.myName == "01" then
            -- Do shit for drop01 -- 
            -- Change score, powersups etc
            event.other:removeSelf( )
            score = score+1
            countDownNumber = countDownNumber + 10
            scoreT.text = "Score: "..score
        end

        if event.other.myName == "02" then
            -- Do shit for drop02 -- 
            -- Change score, powersups etc
            event.other:removeSelf( )
            score = score+1
            scoreT.text = "Score: "..score
        end

        if event.other.myName == "03" then 
            -- Do shit for drop03 -- 
            -- Change score, powersups etc
            event.other:removeSelf( )
            score = score-1
            scoreT.text = "Score: "..score
        end

        if event.other.myName == "04" then
            -- Do shit for drop04 -- 
            -- Change score, powersups etc
            event.other:removeSelf( )
            score = score-1
            scoreT.text = "Score: "..score
        end

    elseif "ended" == event.phase then 
    -- Do shit when leaving surfaces
    end     

    return true

end

------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
------------------------------------------------vvv---------------------------------------------------
function showCountDown (event)
    -- Condition to show and hide countdown
    if countDownNumber <= 1 or score == -1 then
        spawnShit = 0
        countDownNumber = 0
        timer.cancel(timerSpawn)
        timer.cancel(countdownTimer)
        countdownTimer = nil
        highScores[1] = score
        print( 'NO MORE SPAAAAAAAAAAAAAAAWWNS' )
    end

    if countDownNumber >= 1 then
        countDownNumber = countDownNumber -1 
        countDownText.text = countDownNumber
        spawnShit = 1
    end

    return true
end

---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------


function destroy (event)

    if "began" == event.phase then
        event.other:removeSelf( )
        else if "ended" == event.phase then 
        end
        return true
    end
end


--function scene:create( event )
function scene:create( event )
    local sceneGroup = self.view

    -- Initialize the scene here.
    -- Example: add display objects to "sceneGroup", add touch listeners, etc

    --Listeners

    background:addEventListener( "touch", moveEgon )
    bckBtn:addEventListener( "touch", goBack )
    egon:addEventListener( "collision", onCollision )
    destroyAll:addEventListener( "collision", destroy )

    --SceneGroup insert
    sceneGroup:insert( background )
    sceneGroup:insert(egon)
    sceneGroup:insert(bckBtn)
    sceneGroup:insert(drop01)
    sceneGroup:insert(drop02)
    sceneGroup:insert(drop03)
    sceneGroup:insert(drop04)
    sceneGroup:insert(scoreT)
    sceneGroup:insert(countDownText)
    sceneGroup:insert(overlayBg)
    sceneGroup:insert(destroyAll)

end


-- "scene:show()"
function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Called when the scene is still off screen (but is about to come on screen).

    elseif ( phase == "did" ) then
        -- Called when the scene is now on screen.
        -- Insert code here to make the scene come alive.
        -- Example: start timers, begin animation, play audio, etc.
        physics.start( )
        gX = 0
        gY = 10
        physics.setGravity( gX, gY )
        timercount = 10
        spawnShit = 1
        score = 0
        scoreT.text = "Score: "..score

        -- ADD physic bodies ----
        physics.addBody( egon, "static", {density=0.1, friction=0.1, bounce=0.8 } )
        physics.addBody( destroyAll, "static", {density=0.1, friction=0.1, bounce=0.1 } )
        countDownNumber = 10
        if countdownTimer == nil then
            countdownTimer = timer.performWithDelay( 1000, showCountDown, 0 )
        else
        end

        ----------- Timers ------------
        if timerSpawn == nil then
            timerSpawn = timer.performWithDelay(500, spawnObjects, 0 )
        else 
        end

    end
end


-- "scene:hide()"
function scene:hide( event )

    local sceneGroup = self.view
    local phase = event.phase

    if ( phase == "will" ) then
        -- Called when the scene is on screen (but is about to go off screen).
        -- Insert code here to "pause" the scene.
        -- Example: stop timers, stop animation, stop audio, 
        --timer.pause( timerSpawn )
        physics.stop()
        spawnShit = nil
        score = nil
        timerSpawn = nil
        countdownTimer = nil
        overlayBg = nil

        --timer.cancel(timerSpawn)

        physics.removeBody( egon )

    elseif ( phase == "did" ) then
        -- Called immediately after scene goes off screen.
    end
end


-- "scene:destroy()"
function scene:destroy( event )
    local sceneGroup = self.view

    -- Called prior to the removal of scene's view ("sceneGroup").
    -- Insert code here to clean up the scene.
    -- Example: remove display objects, save state, etc.
    bckBtn:removeEventListener("touch", goBack )
    egon:removeEventListener("touch", moveEgon )
end



-- -------------------------------------------------------------------------------

-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )

-- -------------------------------------------------------------------------------

return scene

好的。分数正在保存。问题在于我显示分数文本的方式,因为它没有重新加载,只是重新使用了旧值

修正: 我加了一个

score01.text = highScore[1]

每次场景显示时,当选择屏幕显示时,它会自动更新分数。

I Understand highScore在gameOver函数的范围内,因为您不会得到任何零值索引错误。但您确定要更改的表在范围内吗?也许你正在换另一张高分的本地桌子?你可能想给出更多的细节,也许是你的整个代码?我可以试着发布有问题的代码,当然。我没有收到任何错误,只是好像highScore表被重置为其原始值。@W.B.我已向level1.lua文件添加了一个下载链接。它包含了我用于此目的的所有代码。好吧,让我们重新开始。您如何知道您的分数未分配到该表?你怎么查的?你真正想做什么?将分数分配给表格的第一个元素,也就是你现在正在做的事情,或者将它放在highScore表格的末尾?我使用的是一个级别选择屏幕,有7个级别,它们的文本显示设置为highScore[1],highScore[2],依此类推。因此,当我在级别选择屏幕上时,我可以看到级别1、级别2等等。当我完成第1级时,应该将文本从1改为任何高分[1]。是的,我试着把它分配给第一个元素,作为1级的高分,那么到级将称为高分[2]。我是Lua的新手,所以我不确定是否有更简单的方法,但我喜欢尝试错误。