【问题标题】:Lua / Corona - How do I pass a function as a parameter and then call that functionLua / Corona - 如何将函数作为参数传递,然后调用该函数
【发布时间】:2013-07-22 00:28:57
【问题描述】:

我试图将一个函数作为参数传递给另一个函数。

高级我有创建弹出窗口的代码。当我用新文本更新弹出窗口时,我还想更新用户单击弹出窗口时发生的操作。例如,当我第一次更新弹出窗口时,我可能会将 Action 更改为使用新文本再次显示弹出窗口。当用户点击第二个时

这里有一些示例代码来说明这个概念

function doSomething()
   print("this is a sample function")
end

function createPopup()
    local popup = display.newRect  ... create some display object
    function popup:close()
        popup.isVisible = false
     end
    function popup:update(options)
        if options.action then
            function dg:touch(e)

                 -- do the action which is passed as options.action

            end
        end
    end
    popup:addEventListener("touch",popup)
    return popup
end

local mypopup = createPopup()

mypopup:update({action = doSomething()})

【问题讨论】:

    标签: function lua coronasdk


    【解决方案1】:

    你可以这样称呼它

    function doSomething()
       print("this is a sample function")
    end
    
    function createPopup()
        local popup = display.newRect  ... create some display object
        function popup:close()
            popup.isVisible = false
         end
        function popup:update(options)
            if options.action then
                function dg:touch(e)
                    options.action() -- This is how you call the function
                end
            end
        end
        popup:addEventListener("touch",popup)
        return popup
    end
    
    local mypopup = createPopup()
    
    mypopup:update({action = doSomething})
    

    【讨论】:

    • 不应该在表构造函数中调用doSomething;只需将该字段设置为函数值本身:{action = doSomething}。
    • 我没看到那个
    【解决方案2】:

    我有不同的方法来更改警报文本消息当您单击矩形时查看此代码,它将在您第二次单击它时更改消息

    local Message = "My Message"
    local Title = "My Title"
    local nextFlag = false
    
    
    local function onTap()  
    local alert = native.showAlert( Title, Message, { "OK", "Cancel" }, onComplete )
    end
    
    
    function onComplete( event )
    
        if nextFlag == true then
            if "clicked" == event.action then
               local i = event.index
               Message = "My Message"
               Title = "My Title"
               if 1 == i then
               nextFlag = false
               -- you can add an event here
            elseif 2 == i then
               -- if click cancel do nothing
            end
           end
    
        else
            if "clicked" == event.action then
             local i = event.index
            Message = "Change message"
                Title = "Change Title"
             if 1 == i then
                nextFlag = true
                -- you can add an event here
              elseif 2 == i then
                -- if click cancel do nothing
            end
           end
        end
    
    end
    
    local rectangle = display.newRect(120,200, 100,100)
    rectangle:addEventListener("tap", onTap)
    

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2013-06-03
      • 2021-04-13
      • 2013-07-13
      • 2013-02-13
      • 2017-07-11
      相关资源
      最近更新 更多