Reputation: 63
I want to make an one function execute for one time into the loop Game,
function loopGame(event)
if c1 == true then
---Execute one function
comp()
end
end
The problem is that i put this loopGame into the Runtime with "enterFrame", and the loopGame is exec for frame, then comp is execute more than 100 times.
I want a only execute comp a one time.
Thanks
Upvotes: 2
Views: 857
Reputation: 7390
Just make the flag c1=false
after calling the comp()
method, as:
function loopGame(event)
if c1 == true then
--Execute one function
comp()
c1 = false -- Just add this line and try
end
end
Keep Coding.................. :)
Upvotes: 3
Reputation: 1232
What about having two functions, one that calls comp, and the other which doesn't:
function loopGameAfter(event)
... other stuff ...
end
function loopGameOnce(event)
comp()
... other stuff ...
Runtime:removeEventListener("enterFrame", loopGameOnce)
Runtime:addEventListener("enterFrame", loopGameAfter)
end
Upvotes: 2
Reputation: 652
If you need it to run once, don't use "enter frame" try this:
function loopGame(event)
if c1 == true then
---Execute one function
comp()
end
end
Runtime:addEventListener( "goToLoopGame", loopGame )
And place the dispatch wherever you want it to start the loopGame function:
Runtime:dispatchEvent({ name = "goToLoopGame" })
Upvotes: 3
Reputation: 26744
You can add an upvalue or a global value to keep indicator if the function has already been called:
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
Another option is to use the function itself as the indicator; if it's not used anywhere else (for example, it only does some initialization once), then you can set the function to nil
after it's done (and save some memory):
function loopGame(event)
if c1 == true and comp then
---Execute one function
comp()
comp = nil
end
end
Upvotes: 3