我正在编写将分数保存到“状态”表的代码,它会不断累积,直到到达结果页面。我使用该表是因为它表示表比使用'globals'更好。我在累积时遇到问题,因为当跳转到下一个场景时,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
...
答案 0 :(得分:0)
我在一行中犯了一个小错误,我将其改为:
scoreText = display.newText( storyboard.state.score, 0, 0, native.systemFont, 32 )
答案 1 :(得分:0)
问题是enterScene
事件没有other
字段,因此event.other
为nil
,因此您调用score.update
的代码块会不会在任何一个问题场景中执行。您可以通过在更新中添加一些print
语句来检查这一点。条件应该是别的。