android - 在场景之间使用分数 Corona SDK

标签 android lua sdk coronasdk scene

我有两个场景:game.lua 文件和 restart.lua 文件。一旦 game.lua 文件结束,它就会转移到重新启动屏幕。在重新启动屏幕上,我有“您的当前分数:”和“您的最高分:”以及这些值。但是,这些值不会在每次后续重新启动后自行更新。在我重新启动应用程序之前,高分不会更新,在我重新启动应用程序之前,当前分数不会重置为零。

例如:i) 假设我当前的最高分是 21。我玩了一次游戏并获得了新的最高分:23。我当前的分数变为 23,但我的最高分仍然是 21(直到我重新启动应用程序)。

ii) 我再次玩游戏(没有重启应用程序)并得到 5 分。重启屏幕仍然显示 23 作为我的当前分数。

所以基本上一旦我玩了一次游戏,所有分数就卡住了。

在应用程序中,我使用 self 模块来保存高分(因为这必须是永久的)并将我的当前分数设置为全局。

这是我的游戏场景中的代码:

    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)

这是我重启场景中的代码

------------高分文本----------------

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 ) 

最佳答案

您的代码似乎有点困惑,因为您使用的是 scorehighscoremyscore 但让我们尝试解决这个问题。我将简要介绍这三种方法,但我们只会尝试其中的两种:

    1. 全局得分和高分变量
    1. Game.lua文件
    1. 在场景之间传递参数

1。全局分数和高分变量

这是您现在正在使用的方法,如果您不需要,我不建议使用全局变量,所以让我们跳过此方法并查看其他两个。


2。 Game.lua文件

在此方法中,我们将使用指定的 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()

上面的代码 fragment :

  • 从文件中加载高分
  • 打印当前分数
  • 打印出高分
  • 将当前分数设置为当前高分 + 5
  • 将高分保存回文件

因为我们将 Current Score 设置为 High Score + 5,所以当我们重新启动 Corona Simulator 时,我们将看到该变化。


3。场景间传递参数

当您使用 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/

关于android - 在场景之间使用分数 Corona SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34812343/

相关文章:

android - 将 Firebase 中的重复对象加载到 ListView 中

android - 我无法在实现 ActionTab 监听器的另一个 fragment 的 onTabSelected 中调用 fragment

lua - 从纯Lua创建新的空用户数据

iphone - 具有 6.1 部署目标的 iOS 应用程序

Android Studio 中的 Android SDK 集成

java - 如何使用具有 Java 8 特性的 Realm 数据库

Android 1.6 Mapview 在用户与 map 交互之前不会显示路况

c++ - 来自 Lua 脚本的 nanosleep() 调用暂停了 QT GUI 线程

function - LUA - 从回调中获取变量

c++ - AttachConsole 的实际最低版本要求是什么?