lua - 在 "enterFrame"中执行一次函数

标签 lua coronasdk

我想让一个函数在循环游戏中执行一次,

function loopGame(event)
       if c1 == true then
             ---Execute one function
               comp()
        end
end

问题是我用“enterFrame”将这个 loopGame 放入运行时,loopGame 为帧执行,然后 comp 执行超过 100 次。

我想要一个只执行一次的comp。

谢谢

最佳答案

如果该函数已经被调用,您可以添加一个上值或一个全局值来保持指示符:

local executed = false -- this will be an upvalue for loopGame function
function loopGame(event)
       if c1 == true and not executed then
             ---Execute one function
             comp()
             executed = true -- set the indicator
        end
end

另一种选择是使用函数本身作为指标;如果它没有在其他任何地方使用(例如,它只进行一次初始化),那么您可以在完成后将函数设置为 nil(并节省一些内存):

function loopGame(event)
       if c1 == true and comp then
             ---Execute one function
             comp()
             comp = nil
        end
end

关于lua - 在 "enterFrame"中执行一次函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294966/

相关文章:

c++ - C++ 项目中的 block 状编辑器

nginx - 你如何通过 Nginx 上的 LUA 将 POST 和 GET 传递给脚本?

LUA 和电晕错误 : Attempt To Call Method ' ' (A Nil Value) - Driving Me Crazy

audio - 如何在电晕上播放音频?

c++ - "Undefined reference to"使用 Lua

lua - 如何在 Windows 8.1 上安装 Torch?

json - 将 json 文件与变量连接

string - 如何在Lua中隔离由空格分隔的非英语单词?

object - 在 Lua 中,如何找出存储对象的键?

ios - 如何在多个 Corona SDK 应用程序之间共享通用代码?