我想做出一個函數執行了一次進入循環遊戲,執行函數一次進入「enterFrame事件」
function loopGame(event)
if c1 == true then
---Execute one function
comp()
end
end
的問題是,我把這個loopGame到運行時與「enterFrame事件」, loopGame是可執行的框架,然後comp執行超過100次。
我只想要一次執行comp。
感謝
我想做出一個函數執行了一次進入循環遊戲,執行函數一次進入「enterFrame事件」
function loopGame(event)
if c1 == true then
---Execute one function
comp()
end
end
的問題是,我把這個loopGame到運行時與「enterFrame事件」, loopGame是可執行的框架,然後comp執行超過100次。
我只想要一次執行comp。
感謝
您可以添加的upvalue或全局值,以保持指示燈如果功能已經被稱爲:
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
如果你需要運行一次,不使用「輸入框」試試這個:
function loopGame(event)
if c1 == true then
---Execute one function
comp()
end
end
Runtime:addEventListener("goToLoopGame", loopGame)
和地點派遣無論你想它來啓動loopGame功能:
Runtime:dispatchEvent({ name = "goToLoopGame" })
大約有兩個功能是什麼,1噸帽子調用補償,而另一個沒有:
function loopGameAfter(event)
... other stuff ...
end
function loopGameOnce(event)
comp()
... other stuff ...
Runtime:removeEventListener("enterFrame", loopGameOnce)
Runtime:addEventListener("enterFrame", loopGameAfter)
end
就使標誌c1=false
調用comp()
方法後,如:
function loopGame(event)
if c1 == true then
--Execute one function
comp()
c1 = false -- Just add this line and try
end
end
保持編碼......... ......... :)