0

In Lua there is no simple way to sleep for a desired amount of time. To fix this I created a function that does this for me:

local clock = os.clock
function sleep(n)  -- seconds
   local t0 = clock()
   while clock() - t0 <= n do
   end
end

I then applied it to some code:

player:applyLinearImpulse(0, -14, player.x, player.y)
sleep(1)

The goal was to apply a linear impulse to the ground then freeze the game. However, this simply freezes the game before applying the impulse. In general how can I make sure the previous command is carried out before I sleep?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
user3474775
  • 199
  • 1
  • 2
  • 12
  • 3
    It seems that the `applyLinearImpulse` function takes effect in the next iteration of some main loop, and your `sleep` function prevents you from going back to that main loop. Most event systems/main loops have some form of timers built-in, but we will need to know which event system/main loop you are dealing with to help you adapt your `sleep` function. – siffiejoe Jun 02 '14 at 08:58

1 Answers1

0

In Corona, dynamic actions on objects (like applying an impulse, moving, starting a timer, etc) are really only "enacted" after your code returns. You would have to use timer.performWithDelay(1, function() sleep(1) end).

Once the timer callback is run, the GUI will only be updated upon return from that callback (sleep), so your GUI will be frozen during 1 second. That seems to be the effect you are looking for, though usually you don't want to freeze the GUI.

Oliver
  • 27,510
  • 9
  • 72
  • 103