您不能将功能添加到显示组。由于 sceneGroup 是一个表(就像 lua 中的大部分内容一样),您可以像这样声明 HomePage:
sceneGroup.HomePage = function(params)
-- code for HomePage
end
如果你想通过composer.setVariable和composer.getVariable给composer添加一个函数,你有这个选项。
local composer = require( "composer" )
local scene = composer.newScene()
-- locals
local testFunc = function(hello)
print(hello)
end
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
-- create a variable called "myFuntion" with the value of a reference to testFunc
composer.setVariable( "myFunction", testFunc )
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "did" ) then
-- Called when the scene is now on screen.
composer.getVariable( "myFunction" )("Testing!")
end
end
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
return scene
一旦场景在屏幕上完成动画,这将打印“Testing”。
但是,我看不出这样做的目的。如果您想为您的作曲家场景添加一个功能,我建议您使用这种方法。
local composer = require( "composer" )
local scene = composer.newScene()
-- local forward references for FUNCTIONS should go here
local myFunction
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
-- init functions here
myFunction = function(param)
print("myFunction says "..param)
end
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "did" ) then
-- Called when the scene is now on screen.
myFunction("hello")
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- remove your scene's functions here
myFunction = nil
end
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "destroy", scene )
return scene
如您所见,myFunction 可以在整个场景中调用,没有任何问题。一旦场景在屏幕上完成动画,此示例将打印“myFunction say hello”。
希望这会有所帮助,
乔。