"state"表保存得分的功能无法正常工作。

我正在编写代码,将分数保存到“状态”表中,并将持续累积,直到达到结果页面。我使用表格,因为它比使用“全局变量”更好。我在积分累积方面遇到了问题,因为当跳转到下一个场景时,scoreText会重置为0。

main.lua

display.setStatusBar( display.HiddenStatusBar )
local storyboard = require "storyboard"
storyboard.state = {}
storyboard.state.score = 0
storyboard.gotoScene( "scene_splash" )

Question1.lua

local scoreText

function scene:enterScene(event)
    ...
    scoreText = display.newText( "0", 0, 0, native.systemFont, 32 )
    scoreText:setFillColor( 0,0, 0 )
    scoreText.x = 87
    scoreText.y = 28
    group:insert( scoreText )

    if (event.other == balloons[1])  then
        scene.updateScore()
        print('Ball is colliding')
        balloon1.isVisible = false
        balloonText1.isVisible = false
        audio.play(pop)
        storyboard.gotoScene("correctAnswer1", "fade", 1000)
    end
end

function scene.updateScore()
   storyboard.state.score = storyboard.state.score + 50
   scoreText.text = storyboard.state.score
end

Question2.lua

local scoreText

function scene:enterScene(event)
    ...
    if (event.other == balloons[3])  then
        scene.updateScore()
        print('Ball is colliding')
        balloon3.isVisible = false
        balloonText3.isVisible = false
        audio.play(pop)
        storyboard.gotoScene("correctAnswer2", "fade", 1000)
    end
end

function scene.updateScore()
   storyboard.state.score = storyboard.state.score + 50
   scoreText.text = storyboard.state.score
end

...

点赞
用户3305142
用户3305142

我在一行代码中犯了一个小错误,所以进行了更改:

scoreText = display.newText( storyboard.state.score, 0, 0, native.systemFont, 32 )
2014-03-24 08:09:33
用户869951
用户869951

问题在于 enterScene 事件没有 other 字段,所以 event.othernil,所以你调用 score.update 的代码块在这两个问题场景中都不会被执行。 你可以通过在 update 函数中打一些 print 语句来检查这个问题。这个条件应该是其他的东西。

2014-03-24 14:17:52