【问题标题】:How to :insert an entire function into a group (sceneGroup)如何:将整个函数插入组(场景组)
【发布时间】:2015-07-14 22:04:16
【问题描述】:

我知道如何将单个变量放入组中,但如果我需要组中的一个函数(这样当我去制作场景时,我可以将整个函数插入到 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.

如果您有任何想法或知道如何执行此操作,请告诉我。

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    您不能将功能添加到显示组。由于 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”。

    希望这会有所帮助,

    乔。

    【讨论】:

    • 谢谢乔,我现在想通了,并且在我的应用程序上取得了很大进展,我理解作曲家场景的工作方式比我问这个问题之前做得更好。似乎根本不需要在sceneGroup中添加函数来使程序工作。 :)
    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多