【问题标题】:How can I access,or call a global function inside a function inside a External Library?如何访问或调用外部库内函数内的全局函数?
【发布时间】:2012-10-01 14:18:35
【问题描述】:

我有一个这样的类作为例子:

   --An External Library --UI.lua
    UI = {}
   function UI: new()
    local Group = display.newGroup;

    local inventory_frames = display.newImage("inventorybox.png") ;
    Group :insert( inventory_frames) ;

    function inventory_framesDown()

      local tr_down = transition.to(inventory_frames,{time = 150,alpha = 0, x=0 ,y =8})

    end 


    return Group
    end
    return UI    

现在来自 corona 的实际 scene.lua(使用故事板 API)。

1.local ui=需要“UI.lua” 之后在我的创建场景函数中()(我没有把它放在组场景中的原因是因为我想让它手动消失)

local UI2 = UI:new()

然后在我的退出场景函数中。我想从 UI:new() 中调用函数 inventory_framesDown()。

function scene:exitScene(e)

invent = UI:new() inventory_framesDown() --this dose not work

storyboard.purgeScene("scene2");
storyboard.removeAll()


end

那么如何从外部库调用全局函数内的全局函数呢? 在此先感谢:)

【问题讨论】:

  • 玩了很多之后,我想通了
  • 我在您的代码中看到了 UI:function new() 。这不是有效的 Lua 语法。你的意思是function UI:new() 吗?问一个关于甚至没有编译的代码的问题,可以最大限度地降低得到一个好的答案的可能性。
  • 谢谢,是的,我做到了,我刚刚修好它。这些都是保持清醒的迹象;)

标签: lua coronasdk moai


【解决方案1】:

基本上;

--An External Library --UI.lua


  UI = {}
    function UI:new()
    local Group = display.newGroup;

    local inventory_frames = display.newImage("inventorybox.png") ;
    Group :insert( inventory_frames) ;

    function Group: inventory_framesDown() -- I rewrite the code like this.

      local tr_down = transition.to(inventory_frames,{time = 150,alpha = 0, x=0 ,y =8})

    end 


return Group
end
return UI   

然后在需要库之后在我的 Scene.lua 中。 在 Creat scene function() 我写 local UI2 = UI:new() 和以前一样 然后:

function scene:exitScene(e)

UI2.inventory_framesDown()  --This Works

storyboard.purgeScene("scene2");
storyboard.removeAll()


end

我仍然有点困惑,为什么要这样做?既然有很多方法可以创建类和对象。如果你有更好的解决方案,我很想知道再次感谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2023-03-12
    • 2021-11-06
    • 1970-01-01
    相关资源
    最近更新 更多