如何:将整个函数插入一个组(sceneGroup)

时间:2015-07-13 17:07:01

标签: lua corona

我知道如何将单个变量放入一个组但是如果我需要组中的一个函数(这样当我去创建一个场景时,我可以将整个函数插入到sceneGroup而不是输入我是如何插入的?

代码:

function scene:create(event) 
    local sceneGroup = self.view
end

function scene:show(event)
    local sceneGroup = self.view
    local phase = event.phase

    if (phase == "will") then
     sceneGroup:insert(HomePage()) --- this is what I have tried

    elseif (phase == "did") then

    end
end


scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)

return scene

scene1.lua:140:错误:预期的表格。如果这是一个函数调用,你可能已经使用了'。'而不是':'

这是我收到的错误消息。

我也试过

sceneGroup:insert(HomeGroup) --- this is without the () at the end, at it still fails to work.

如果您有任何想法或知道如何做,请告诉我。

1 个答案:

答案 0 :(得分:0)

您无法将功能添加到显示组。由于sceneGroup是一个表(就像lua中的所有内容一样),你可以像这样声明HomePage:

sceneGroup.HomePage = function(params)
    -- code for HomePage
end

如果你想通过composer.setVariable和composer.getVariable向作曲家添加一个函数,你有这个选项。

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

一旦场景完成动画在屏幕上,这将打印“测试”。

然而,我没有看到这样做的目的。如果你想要一个作曲家场景的功能,我建议这个方法。

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”。

希望这有帮助,

乔。