composer.showOverlay()

类型 函数
composer.*
返回值
修订 版本 2024.3703
关键词 composer, 场景, 覆盖, showOverlay
另请参阅 composer.hideOverlay()

概述

此函数在当前活动场景上方加载一个覆盖场景 —即“父”场景 —并保持父场景不变。

显示覆盖场景时,将向覆盖场景分派一个特定于覆盖场景的 scene `event` 参数,event.parent。此参数为您提供对父场景对象的引用,以便您可以在其中调用函数/方法。有关用法示例,请参阅 composer.hideOverlay() 文档。

此函数支持 composer.gotoScene() 可用的所有场景过渡效果。

要防止覆盖场景上的触摸传递到底层对象(如按钮和小部件),请将 `isModal` 选项设置为 `true`。

注意事项

任何时候都只能显示一个覆盖场景。如果您调用 composer.gotoScene()composer.removeHidden() 或尝试显示其他覆盖场景,当前覆盖场景将被隐藏。因此,您应该在尝试显示其他覆盖场景之前隐藏当前覆盖场景,或者通过 composer.gotoScene() 更改活动场景。

语法

composer.showOverlay( sceneName [, options] )
sceneName (必填)

字符串 要作为覆盖场景加载的场景的名称。

options (可选)

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

覆盖选项

`options` 表可以包含与覆盖场景相关的各种选项

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

布尔值 当设置为 `true` 时,这可以防止触摸从覆盖场景传递到底层场景中的对象。默认为 `false`。

effect (可选)

字符串 指定覆盖场景过渡的效果。有关有效选项列表,请参阅 composer.gotoScene()。如果未指定效果,则覆盖场景将立即出现。

time (可选)

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

params (可选)

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

示例

------------------------------------------------------------------------------
-- In "scene1.lua" (parent scene)
------------------------------------------------------------------------------

local composer = require( "composer" )

local scene = composer.newScene()

-- Custom function for resuming the game (from pause state)
function scene:resumeGame()
    -- Code to resume game
end

-- Options table for the overlay scene "pause.lua"
local options = {
    isModal = true,
    effect = "fade",
    time = 400,
    params = {
        sampleVar = "my sample variable"
    }
}

-- By some method such as a "pause" button, show the overlay
composer.showOverlay( "pause", options )

return scene


------------------------------------------------------------------------------
-- In "pause.lua"
------------------------------------------------------------------------------

local composer = require( "composer" )

local scene = composer.newScene()

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase
    local parent = event.parent  -- Reference to the parent scene object

    if ( phase == "will" ) then
        -- Call the "resumeGame()" function in the parent scene
        parent:resumeGame()
    end
end

-- By some method such as a "resume" button, hide the overlay
composer.hideOverlay( "fade", 400 )

scene:addEventListener( "hide", scene )
return scene