I am trying to make a script sleep and I am new to this language like 5 minutes new. How do you make it sleep or pause for 10 seconds? I found this
function sleep(n)
os.execute("sleep " … tonumber(n))
end
But how do you define (n)?
I am trying to make a script sleep and I am new to this language like 5 minutes new. How do you make it sleep or pause for 10 seconds? I found this
function sleep(n)
os.execute("sleep " … tonumber(n))
end
But how do you define (n)?
Hey scottoliver,
I don’t know about the code you posted, but it seems os.execute does not work reliable on all devices (depending on your input).
The important question is, what do you want to do. Do you really want to like freeze your app like your code suggests or do you just want to wait some time to execute some code?
If it’s the second you should take a look at timers, which call a function after a given delay.
timer.performWithDelay( delay, listener )
Follow this link to the documentation for further information:
https://docs.coronalabs.com/api/library/timer/performWithDelay.html
Sleeping is not how Corona works. It’s an event driven system. You should learn to use timers to schedule things as @torbenzatzlaff said or use things like collisions (if you’re using Physics), touch and tap events to cause actions to happen.
I downloaded Corona today and I’m still experimenting it, but to the point:
if I wanted to have a timer count from 3 to 1 yielding a second on each number I’d have to do something like
local textObj = display.newText("",160,0,"Arial",50) local setText = function(obj,t) obj.text = t end for i = 0,3 do timer.performWithDelay(1000\*i,function() setText(textObj,3-i) end) end
It feels stupid to define a function for each little thing I need, when I could just do
local textObj = display.newText("",160,0,"Arial",50) for i = 3,1,-1 do textObj.text = i wait(1) -- sleep(1000) end
I’ve got 4 years with lua on ROBLOX so I’m probably just be used to their system. Also I’ve got no experience on yielding in other languages (well I just know a bit of PHP) so I don’t know how they handle that. I would love to hear from someone if the first piece of code is even the correct way to do it.
You’re close:
local textObj = display.newText("",160,0,"Arial",50) local ticks = 3 local setText = function(obj,t) obj.text = t end timer.performWithDelay( 1000, function() setText(textObj, ticks); ticks = ticks - 1; end, 3)
This method tells the timer to run 3 times, with a 1 second delay.
Hey scottoliver,
I don’t know about the code you posted, but it seems os.execute does not work reliable on all devices (depending on your input).
The important question is, what do you want to do. Do you really want to like freeze your app like your code suggests or do you just want to wait some time to execute some code?
If it’s the second you should take a look at timers, which call a function after a given delay.
timer.performWithDelay( delay, listener )
Follow this link to the documentation for further information:
https://docs.coronalabs.com/api/library/timer/performWithDelay.html
Sleeping is not how Corona works. It’s an event driven system. You should learn to use timers to schedule things as @torbenzatzlaff said or use things like collisions (if you’re using Physics), touch and tap events to cause actions to happen.
I downloaded Corona today and I’m still experimenting it, but to the point:
if I wanted to have a timer count from 3 to 1 yielding a second on each number I’d have to do something like
local textObj = display.newText("",160,0,"Arial",50) local setText = function(obj,t) obj.text = t end for i = 0,3 do timer.performWithDelay(1000\*i,function() setText(textObj,3-i) end) end
It feels stupid to define a function for each little thing I need, when I could just do
local textObj = display.newText("",160,0,"Arial",50) for i = 3,1,-1 do textObj.text = i wait(1) -- sleep(1000) end
I’ve got 4 years with lua on ROBLOX so I’m probably just be used to their system. Also I’ve got no experience on yielding in other languages (well I just know a bit of PHP) so I don’t know how they handle that. I would love to hear from someone if the first piece of code is even the correct way to do it.
You’re close:
local textObj = display.newText("",160,0,"Arial",50) local ticks = 3 local setText = function(obj,t) obj.text = t end timer.performWithDelay( 1000, function() setText(textObj, ticks); ticks = ticks - 1; end, 3)
This method tells the timer to run 3 times, with a 1 second delay.