Could someone please explain how to slow down this simple code that counts from 1 to 100
-- Counting
-- Use this function to perform your initial setup
function setup()
x=1
end
-- This function gets called once every frame
function draw()
-- This sets a green background colour
background(65, 113, 40, 255)
-- Do your drawing here
-- The default font is ok
fontSize(512)
fill(255) -- White numbers
if x <= 100 then
text(x,WIDTH/2,HEIGHT/2)
x=x+1
end
end
@steep Here’s another way to do it. Since the draw function runs approx. 60 times per second, I added 1/60 to x each time. That gives you about a 1 second delay.
-- Counting
function setup()
x=0
end
function draw()
background(65, 113, 40, 255)
fontSize(512)
fill(255)
if x <= 100 then
text(string.format("%d",x),WIDTH/2,HEIGHT/2)
x=x+1/60 -- about a 1 second delay
end
end
You guys are quicker than the code.
I didn’t expect so many alternatives. Thanks I’ll give them all a try.
The concept of a programme running around uncontrolled 60 times a second takes a bit of understanding and just where to put the ‘brake’ in presents another problem.
@Steep After you program in Codea for awhile, you’ll get used to the 60 FPS of the draw routine. In fact, you won’t even worry about it, because it will seem natural and you’ll wonder how you programmed without it.
You could be right dave1707. I look at Codea/Lua as a touch on the ‘brake’. At the end of the day we need to understand what it is we are trying to control. The code has slowed down the process of getting a number on the screen. It’s the things that have happened to allow the number to be created in the first place that leaves me cold. Loops within loops, enough to drive you ‘Luapy’.
My start point is limited knowledge of BBC Basic.
I find your code the easiest to follow . You modified my code to allow it to be slowed down. I’m looking at changing the colour of successive numbers.
@dave1707, no, I meant to just put DeltaTime. I copied this from a project I have where I had (DeltaTime/2)*moveamt to make it move slower, and when I copied it over I put a one instead of deleting it xD. I should not post when I am not fully awake. Fixed in the snippet above
Monkeyman32123 - I don’t know where/how to implement your code. Would you please stitch it into my program in post 1 and repost.
As understand it the ‘system’ has already been slowed down significantly because three transformations are taking place.
1- Codea telling Lua to operate a bit slower
2 - Lua telling C what it wants to do
and 3 - C telliing Machine code to sort it out.
We are intervening at stage 1 by telling Codea to slow down what it is we want it to do.
For instance, if you wanted the transformation from x=1, to x=100 to take (very close to) three seconds you would do:
function setup()
x=1
dt=0
end
function draw()
background(65, 113, 40, 255)
fontSize(30)
fill(255)
if x < 100 then
x=x+((99/3)*DeltaTime)
dt = dt + DeltaTime
output.clear()
print("Time Taken: "..dt)
if x>100 then x=100 end
end
text(x,WIDTH/2,HEIGHT/2)
end
In the line x=x+((99/3)*DeltaTime) the 99 is the total amount x is going to change (100-1), the three is the amount of seconds you want the transformation to take; so (99/3) is in amount per second and DeltaTime is in seconds per frame. When you multiply the two you get some value in amount this frame.
Thanks MonkeyMan3213 but unfortunately your code destroys the counting effect. I tried to use the larger font size from my code but for some reason your code doesn’t like it. It does make my point though that it is difficult to decide where/when to apply the brake.
I’ve changed dave1707’s code to count 1 to 10 and I am finding it a bit tricky to change the colour of alternate numbers.
PS- I taught myself to code in BBC Basic by playing around with numbers in this way.
That’s because if text is larger than the screen it won’t display on the screen, so you need to cut off decimals to get it to fit on the screen with larger fonts. Here’s a version that can be much larger.
function setup()
x=1
dt=0
end
function draw()
background(65, 113, 40, 255)
fontSize(250)
fill(255)
if x < 100 then
x=x+((99/3)*DeltaTime)
dt = dt + DeltaTime
output.clear()
print("Time Taken: "..dt)
if x>100 then x=100 end
end
text(math.floor(x),WIDTH/2,HEIGHT/2)
end
@Dave1707’s code works fine in programs running 60fps, which is a majority of situations, but mine works better in situations where there may be lag as it is directly influenced by the difference in time between frames instead of amount of frames running per second. (that said, mine, like his, is not perfect in programs under extremely heavy lag)
Also, it’s worth noting that large font sizes introduce considerable lag. If you text() something at 512 fontsize and it’s value changes every frame you get approximately 11 FPS. In this case, mine would still stop at about 3 seconds, whereas Dave’s would put the brake on at about 18 seconds (if his was modified to take 3 seconds). So keep fontsizes in mind, they can have a huge effect.
displayMode(OVERLAY)
function setup()
x=1
dt=0
end
function draw()
background(65, 113, 40, 255)
fontSize(500)
fill(255)
if x < 100 then
x=x+(99/100*DeltaTime)
dt = dt + DeltaTime
if x>100 then x=100 end
end
text(string.format("%d",x),WIDTH/2,HEIGHT/2)
output.clear()
print(dt)
end
Above is my version modified to take 100secs like @Dave1707’s
@Steep Here’s another delay for your code. There isn’t going to be only one routine to do a delay. Depending on the program the delay is in, and what needs to be delayed, will determine how a delay routine will be written. You seen several ways here of slowing down what you want to do in this program. Some routines work better than others, but the final delay you write will be determined by everything you’re trying to do in your final program. Without knowing everything you want to do in that program, we can’t show you the delay routine that’s going to work.
-- Counting
displayMode(FULLSCREEN)
function setup()
x=0
dt=0
end
function draw()
background(65, 113, 40, 255)
fontSize(512)
if x <= 100 then
if delay(1) then -- delay for 1 second
x=x+1
fill(math.random(255),math.random(255),math.random(255))
end
text(string.format("%d",x),WIDTH/2,HEIGHT/2)
end
end
function delay(limit)
dt=dt+DeltaTime
if dt>limit then
dt=0
return true
end
return false
end
Thanks very much guys. I recognise that at the end of the day it is fitness for purpose.
You’ve given me quite a bit to think about. I hope you will continue to keep an eye on me.
Steep
BBC Basic used a repeat until loop. I used to find out where to put it by trial and error.
@Steep Codea has the “repeat” loop that can be used, but I guess the draw function can be considered a repeat forever loop. Codea is more of an interrupt driven program. The program starts and then draw() goes into an infinite loop. While in that loop, it looks for interrupt information from the keyboard, screen touch, ipad tilts, camera input, program code, etc. It’s up to you to process those inputs as needed and do whatever you want with them. You can compare that to your life. You get up in the morning, process any interrupt information throughout the day, go to sleep, get up in the morning, repeat.