我有两个场景:game.lua文件和restart.lua文件。一旦game.lua文件结束,它就会转移到重启屏幕。在重新启动屏幕上,我有您当前的分数:'和你的高分:'以及价值观。但是,每次后续重启后,值都不会自行更新。高分不会更新,直到我重新启动应用程序并且当前分数不会重置为零,直到我重新启动应用程序。
所以例如:i)说我当前的高分为21分。我玩一次游戏并获得新的高分:23。我目前的分数是23,但我的高分仍然是21(直到我重新启动应用程序)。
ii)我再次播放(没有重新启动应用程序)并获得5分。重启屏幕仍显示23作为我当前的分数。
所以基本上一旦我玩了一次游戏,所有分数都会被卡住。
在应用程序中,我使用自我模块来保存高分(因为这必须是永久性的)并将我当前的分数设置为全局。
以下是我游戏场景中的代码:
highscore = loadFile("score.txt")--the ego module loads this file for me
score = 0--kept global to use in restart screen
local scoreText = display.newText(score,W,0)--create text
sceneGroup:insert(scoreText)
local function checkForFile()
if highscore == "empty" then
highscore = 0--gives highscore number if file is empty
saveFile("score.txt", highscore)--saves new highscore
end
end
checkForFile()
local function onObjectTouch(event)
if(event.phase == "began") then
score = score+1--increment score on touch
scoreText.text = score
if score>tonumber(highscore) then
saveFile("score.txt",score)--if new highscore, save score as highscore
end
vertical = -150--set ball's velocity
ball:setLinearVelocity(0,vertical)
print(ball.x .. " " .. event.x)
end
end
Runtime:addEventListener("touch", onObjectTouch)
这是重启场景中的代码
------------ highScore Text ----------------
myscore = loadFile("score.txt")--load highscore file
local highscoretext = display.newText( "Your high score:"..myscore, 0, 0, native.systemFontBold, 20 )
highscoretext:setFillColor(1.00, .502, .165)
--center the title
highscoretext.x, highscoretext.y = display.contentWidth/2, 200
--insert into scenegroup
sceneGroup:insert( highscoretext )
--------------yourscore----------------
local yourscoretext = display.newText( "Your score:"..score, 0, 0, native.systemFontBold, 20 )
yourscoretext:setFillColor(1.00, .502, .165)
--center the title
yourscoretext.x, yourscoretext.y = display.contentWidth/2, 230
--insert into scenegroup
sceneGroup:insert( yourscoretext )
答案 0 :(得分:1)
您的代码似乎有点令人困惑,因为您使用的是score
,highscore
和myscore
,但我们可以尝试解决此问题。我将简要解释这三种方法,但我们只会尝试其中两种方法:
这是您现在使用的方法,如果您不需要,我建议您不要使用全局变量,让我们跳过此方法并查看其他两个。
在这种方法中,我们将使用指定的game.lua文件来存储所有数据。请阅读Corona上关于 Modular Classes 如何在Lua中工作的博客文章: https://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/
我们将不在此示例中使用元表,但我们将创建一个game.lua文件,我们可以从Corona项目中的任何其他lua或场景文件调用该文件。这样,我们就可以将分数和高分保存在一个地方,并且可以非常轻松地保存和加载高分。因此,让我们创建 game.lua 文件:
local game = {}
-- File path to the score file
local score_file_path = system.pathForFile( "score.txt", system.DocumentsDirectory )
local current_score = 0
local high_score = 0
-----------------------------
-- PRIVATE FUNCTIONS
-----------------------------
local function setHighScore()
if current_score > high_score then
high_score = current_score
end
end
local function saveHighScore()
-- Open the file handle
local file, errorString = io.open( score_file_path, "w" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Write data to file
file:write( high_score )
-- Close the file handle
io.close( file )
print("High score saved!")
end
file = nil
end
local function loadHighScore()
-- Open the file handle
local file, errorString = io.open( score_file_path, "r" )
if not file then
-- Error occurred; output the cause
print( "File error: " .. errorString )
else
-- Read data from file
local contents = file:read( "*a" )
-- Set game.high_score as the content of the file
high_score = tonumber( contents )
print( "Loaded High Score: ", high_score )
-- Close the file handle
io.close( file )
end
file = nil
end
-----------------------------
-- PUBLIC FUNCTIONS
-----------------------------
function game.new()
end
-- *** NOTE ***
-- save() and load() doesn't check if the file exists!
function game.save()
saveHighScore()
end
function game.load()
loadHighScore()
end
function game.setScore( val )
current_score = val
setHighScore()
end
function game.addToScore( val )
current_score = current_score + val
print("Current score", current_score)
setHighScore()
end
function game.returnScore()
return current_score
end
function game.returnHighScore()
return high_score
end
return game
在您的场景文件中调用game.lua
,如下所示(位于场景顶部):
local game = require( "game" )
并且您可以在需要时调用不同的游戏方法,如下所示:
game.load()
print("Current Score: ", game.returnScore())
print("High Score: ", game.returnHighScore())
game.setScore( game.returnHighScore() + 5 )
game.save()
上面的代码段:
因为我们将当前得分设置为高分+5,我们将在重新启动电晕模拟器时看到更改。
使用composer.gotoScene
切换场景时,您还可以添加如下参数:
local options =
{
effect = "fade",
time = 400,
params =
{
score = 125
}
}
composer.gotoScene( "my_scene", options )
并在被调用的场景中像这样访问它们:
function scene:create( event )
local sceneGroup = self.view
print("scene:create(): Score: ", event.params.score )
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
print("scene:show(): Score: ", event.params.score )
end
有关此方法的文档:https://coronalabs.com/blog/2012/04/27/scene-overlays-and-parameter-passing/