我想让一个函数在循环游戏中执行一次,
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/