Lua 如何:将整个函数插入组(场景组)
我知道如何将单个变量放入一个组中,但如果我需要组中的一个函数,以便在制作场景时,我可以将整个函数插入到场景组中,而不是单独输入每个变量,我将如何插入 代码: 场景1.lua:140:错误:应为表。如果这是一个函数调用,则可能使用了“.”而不是“:” 这是我收到的错误消息 我也试过了Lua 如何:将整个函数插入组(场景组),lua,coronasdk,Lua,Coronasdk,我知道如何将单个变量放入一个组中,但如果我需要组中的一个函数,以便在制作场景时,我可以将整个函数插入到场景组中,而不是单独输入每个变量,我将如何插入 代码: 场景1.lua:140:错误:应为表。如果这是一个函数调用,则可能使用了“.”而不是“:” 这是我收到的错误消息 我也试过了 sceneGroup:insert(HomeGroup) --- this is without the () at the end, at it still fails to work. 如果您有任何想法或知道如
sceneGroup:insert(HomeGroup) --- this is without the () at the end, at it still fails to work.
如果您有任何想法或知道如何操作,请告诉我。您不能向显示组添加功能。由于sceneGroup是一个表,与lua中的大部分内容类似,所以可以这样声明主页:
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
这将在场景在屏幕上完成动画制作后打印测试
然而,我看不出这样做的目的。如果您想为composer场景创建一个函数,我建议使用这种方法
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在场景在屏幕上完成动画制作后说hello
希望这有帮助
Joe。谢谢Joe,我现在明白了,并且正在向我的应用程序迈进,我比问这个问题之前更了解作曲家场景的工作原理。似乎根本不需要将函数添加到sceneGroup中即可使程序正常工作:
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