类型 库 版本 版本 2024.3703 关键词 composer, 场景 另请参阅 Composer 库 (指南)
Composer 是 Solar2D 中官方的场景(屏幕)创建和管理库。该库为开发者提供了一种创建和转换各个场景的简便方法。
Composer 库中的主要对象是 scene 对象。这是一个响应特定事件的事件监听器,它包含一个唯一的 self.view 属性,该属性是对与场景关联的 显示组 的引用。您应该将与场景相关的视觉元素插入到此 self.view 中。
以下模板可用于创建新的场景文件。请注意,此模板包含场景中所有潜在事件的侦听器函数,但您只需包含要处理的事件的侦听器。
local composer = require( "composer" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
local sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is on screen (but is about to go off screen)
elseif ( phase == "did" ) then
-- Code here runs immediately after the scene goes entirely off screen
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene
如果您要添加/修改场景过渡效果或扩展功能,请从 GitHub 下载源代码并将其包含在您的项目中(请参阅 使用外部模块)。