所以我正在制作一款有很多值可以保存的游戏。我使用Rob Miracle的一个模块,可以很容易地保存和加载表来实现这两个表。一个被命名为#34; gamestuff"另一个名为" leveltries"。
在一个场景中,我声明了这些表的值(只有在之前没有保存过它们)
gamestuff.level = 1
gamestuff.sound = true
gamestuff.name = "user"
gamestuff.highscore = 0
gamestuff.lastscore = 0
gamestuff.volume = 0.5
gamestuff.lastlevel = 1
gamestuff.won = false
gamestuff.bouncesleft = 0
local leveltries = {
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
{tries =0, wins = 0, fails = 0, retries = 0, bouncesleft = 0, bestbounces = 0, score = 0},
}
loadsave.saveTable(leveltries, "leveltries.json")
loadsave.saveTable(gamestuff, "gamestuff.json")
除了当我尝试进入重启级别场景和第一级之后的获胜场景时,此工作正常。
但它让我困惑,因为在第一级我尝试
print("Fails: "..leveltries[gamestuff.lastlevel].fails)
并且有效
但是当我在重新启动器场景中尝试它时,它并没有:
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
local loadsave = require("loadsave")
local gamestuff = {}
local leveltries = {}
local options =
{
effect = "fade",
time = 400
}
local function loader()
gamestuff = loadsave.loadTable("gamestuff.json")
leveltries = loadsave.loadTable("leveltries.json")
print("loaded")
end
local function save()
loadsave.saveTable(gamestuff, "gamestuff.json")
loadsave.saveTable(leveltries, "leveltries.json")
end
function scene:createScene(event)
gamestuff = {}
leveltries = {}
end
function scene:enterScene(event)
loader()
print("Fails"..leveltries[gamestuff.lastlevel].fails) --says the error is here
local a = leveltries[gamestuff.lastlevel].fails + 1
leveltries[gamestuff.lastlevel].fails = a
timer.performWithDelay( 50, function()
save()
storyboard.gotoScene("level"..tostring(gamestuff.lastlevel), options)
end, 1 )
end
function scene:exitScene(event)
local group = self.view
storyboard.removeScene("restarter")
end
function scene:destroyScene(event)
gamestuff = nil
leveltries = nil
options = nil
end
scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )
return scene
有谁可以告诉我为什么上面的代码不起作用?
可能是因为我没有正确删除游戏"和" leveltries"在level1?
我尝试放入一些打印件。在1级场景中:
一切正常:
print(gamestuff.lastlevel)
print(leveltries[gamestuff.lastlevel].fails)
print(leveltries[gamestuff.lastlevel])
但是在重启器场景中,只有:
print(gamestuff.lastlevel) --which prints 1 and is right
作品
print(leveltries [gamestuff.lastlevel]),在restarter场景中打印nil
我不知道这是怎么回事。我在两个场景中以相同的方式同时加载相同的表格并以相同的方式使用它们。
可能是因为我错误地删除了表格吗?
在场景一中,这些是我的exitScene和removeScene:
function scene:exitScene(event)
Runtime:removeEventListener("touch", shoot)
menub:removeEventListener("touch", gotomenu)
restartb:removeEventListener("touch", restart)
goal:removeEventListener("collision", win)
ball:removeEventListener("collision", hit)
ball:removeEventListener("collision", grow)
Runtime:removeEventListener("enterFrame", twinkle)
Runtime:removeEventListener("enterFrame", redraw)
end
function scene:didExitScene(event)
--storyboard.removeScene("level1")
end
function scene:destroyScene(event)
bg:removeSelf()
ball:removeSelf()
goal:removeSelf()
topwall:removeSelf()
bottomwall:removeSelf()
rightwall:removeSelf()
leftwall:removeSelf()
menub:removeSelf()
bounces = nil
bouncestext:removeSelf()
taps = nil
restartb:removeSelf()
tuttext:removeSelf()
gamestuff = nil
bg = nil
ball = nil
goal = nil
topwall = nil
bottomwall = nil
rightwall = nil
leftwall = nil
menub = nil
bouncestext = nil
restartb = nil
tuttext = nil
options = nil
trailprops = nil
for i = 1, 3 do
wallColor[i] = nil
end
end
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("didExitScene", scene)
scene:addEventListener("destroyScene", scene)
答案 0 :(得分:0)
我认为问题是,你在场景的开头声明了gamestuff和leveltries数组。然后你再次在创建场景中声明它。你再次覆盖同一个数组。我认为它没有任何意义。删除在创建场景中声明的数组并尝试。