composer.gotoScene()

类型 函数
composer.*
返回值
修订 2024.3703 版
关键词 composer、场景切换、gotoScene

概述

此函数用于过渡到特定场景。调用时,将向当前场景(如果存在)分派 hide 事件。如果自述视图组尚未针对指定的目标场景存在,则将向该场景分派 create 事件,然后分派 show 事件。

语法

composer.gotoScene( sceneName [, options] )
sceneName (必需)

字符串. 要转到的场景名称。在很多情况下,这是场景的 Lua 文件的名称(没有 .lua 扩展名)。

options (可选)

表格. 此表格包含几个与过渡有关的选项,例如效果类型、自定义参数等。有关此表格的可接受格式,请参阅 场景选项

场景选项

options 表格可以包含与目标场景相关的各种选项

local options =
{
    effect = "fade",
    time = 400,
    params = {
        sampleVar1 = "my sample variable",
        sampleVar2 = "another sample variable"
    }
}
effect (可选)

字符串. 指定场景过渡的效果。请参阅 过渡效果,了解有效选项列表。如果没有指定效果,场景过渡将立即发生。

time (可选)

数字. 如果已指定有效效果,则效果的时间持续时间。默认值为 500 毫秒。

params (可选)

表格. 一个可选的表格,包含应传输到场景的任何自定义数据。在指定的场景中,可以通过 create 事件或 show 事件中的 event.params 访问此数据。

过渡效果

options 表格的 effect 键支持以下字符串值

实例

转到其他场景
local composer = require( "composer" )

local currentScore = 200

-- Later...
local options = {
    effect = "fade",
    time = 800,
    params = { level="Level 1", score=currentScore }
}
composer.gotoScene( "results", options )
重新加载当前场景
local composer = require( "composer" )

-- Later...
local currScene = composer.getSceneName( "current" )
composer.gotoScene( currScene )