I was wondering if codea had a pause function. Like so then the code would stop for 0.01 seconds or something. The original draw function goes 60 times a second. For the code i’m using its to slow, but using while x < HEIGHT do is to fast and crashes my ipad. Is there a way i could make it pause?
@dave1707 It always will work the same, there’s no way it could work differently in setup() or draw(). The result you’re getting I said would happen. It’s an error in your code, not tween.delay().
There are many uses I can think of for tween.delay() in setup(), draw(), touched(touch), keyboard(key), or collision(collide).
You may not be trying to argue, but it doesn’t seem like you’re reading what I’m saying.
@SkyTheCoder I am reading what you’re saying, but it doesn’t seem like you’re looking at the examples to see what’s happening. I agree with you that tween.delay() is delaying from the start of each draw call, then executing, but that’s not the intended result. Draw() is running at 60fps. If something in draw() or something called from draw() needs to wait 3 seconds each time before executing, would I use your tween.delay() code or would I use my tween.delay() code. I changed the delay to 3 seconds in both examples. Get out your stopwatch and run these 2 examples. Tell me which one is waiting each time for 3 seconds before executing (printing).
SkyTheCoder tween.delay() in draw.
function setup() print("Hello world!") end function draw() tween.delay(3, function() print("SkyTheCoder draw 3 second delay!") end) end
dave1707 tween.delay() in draw.
function setup() print("Hello!") end function draw() background(40, 40, 50) if id==nil then id=tween.delay(3,bbb) end end function bbb() print("dave1707 draw 3 seconds delay.") id=nil end
Probably the best way is to use a timer or counter
A counter could be used to (say) only draw every second or third time
A timer could be used to draw every X seconds
@shipmaster4000 your crash is not normal. It is due to the fact you call your ‘while’ too often. You refer to x so this should probably be done only once in the draw() or touched() function, without looping with a ‘while’ but more likely with an ‘if’. CODEA will do the looping for you when the draw() is called or when you move your finger which call touched(). Check if what i say make sense for you.
I mean is there a wait function in codea? Like i know alot of codes have a wait(time) Api. Does codea not have one?
@Shipmaster4000 There isn’t a wait function like you’re thinking of. The only way to wait in Codea is to not execute code by putting in a counter, timer, etc to skip around it. That’s not a problem to do, but you need to code for it. What do you want to wait for. There is a pause command, but that’s if you’re using physics.objects and not normal code.
@shipmaster4000 really, normally you dont need a timer for most cases. If you think you need a timer, you (probably) have not understood the logic of codea: the draw() function gives the pace 60 times per second, you just have to program with this input. It takes some time at the beginning to figure out how to do, but once you,ve got it, it is quite easy and natural. As you, i started with the same questions. Hope this helps.
I know I have used this timer quite often. The way I wrote my code. I have it in a for loop. Where it goes through each cube i have and moves it one pixel forward. The broblem is. After X ammount of cubes The game slows down. The natural 60 times a second Becomes too slow to move them all. So i run intpo the problem of it running too slow. I try to speed it up and it crashes my game. A nice example of a while loop acually being used would be nice
@shipmaster4000 - what I do is use a timer, like this. It should work no matter how fast or slow Codea is drawing.
--in setup timeToNextCube = cubeInterval --time to first cube, in seconds --in draw timeToNextCube = timeToNextCube - DeltaTime --count timer down if timeToNextCube < 0 then --time for next cube --put code here to launch next cube, then... timeToNextCube = timeToNextCube + cubeInterval --reset timer for next cube end
If draw running at 60 FPS is too slow, you need to speed up your code, because it won’t go any higher.
If it’s not moving fast enough because of lag from drawing, use DeltaTime. Instead of something like
x = x + 5, do
x = x + 5 * 60 * DeltaTime.
And no, there’s no way in Codea to wait on a line for [X] seconds. You can delay it a slightly different way, though. I’m shocked no one here has yet, not even Ignatz, recommended tween.delay(). I’m told it’s the best way to make a timer or delay. Just call
tween.delay(seconds, function to be called after time is up). Example:
function setup() print("Hello world!") tween.delay(1, function() print("Hello world after 1 second!") end) end
And I believe by “crash my iPad” you mean Codea freezes and quits after a little bit, not just a Lua error such as stack overflow. That’s what happens when you have a loop that goes on infinitely. Codea waits for draw to finish until updating the screen and calling draw again. If you had something like
while true do print("Blah.") end, it’s the same as
while x < HEIGHT do print("Blah.") end because you never change
x so the test eventually is false and it breaks the loop.
@SkyTheCoder Tween delay works fine if it’s called one time. But if you put tween.delay in draw or a function called by draw, then it doesn’t work as you suggested. See example code. As far as tween.delay not being suggested before, see the following post.
function setup() print("Hello world!") tween.delay(1, function() print("Hello world after 1 second!") end) end function draw() tween.delay(1, function() print("Hello world after 1 second!") end) end
@dave1707 No, that’s working as I suggested. If you call it in
draw(), you’re programming it to wait one second from when that frame was drawn, and then to print stuff. It’s in draw, so it’s called once every frame. So, it waits one second from when draw was initially ran, and then spams the print function. I’m not sure what you intended it to do, but that is not an error with
tween.delay, it’s your code. If you wanted it to be called on an interval of one second, @toadkick has made multiple functions for that. Look back at that discussion, right after he initially recommended
tween.delay, he explained how to make a loop.
@SkyTheCoder If you look farther down in that discussion, you’ll see where I show how it’s supposed to be coded to work in draw. You can’t put tween.delay() in the draw function the way you suggested and expect it to work. As you said, it just spams the print function. The code I show in the other discussion only prints every 3 seconds the way it’s set up in that tween.delay() to do.
@dave1707 I didn’t suggest you could put tween.delay in the draw function. This argument has no point. Read my comment again, all the way through.
If you scroll down below your comment, there’s @toadkick’s interval functions.
@SkyTheCoder I’m not trying to argue, I’m just trying to point out that the tween.delay() you use in setup() doesn’t work the same in draw(). If tween.delay() isn’t going to be used in or from draw(), then where. It’s useless in setup(). Since draw() runs at 60fps, all functions that are called from draw() also run at 60 fps (unless you bog them down). So if you want something in one of those functions to run at a slower rate, you need to add a timer or tween.delay(). But tween.delay() can’t be used in draw() or functions called from draw() the way you show in your setup() example, or you’ll create 60 tween.delays() every second. Here’s an example showing how a 3 second tween.delay() can be used in all 3 functions. Tap the screen for the touch delay.
function setup() print("Hello!") if a==nil then a=tween.delay(3,aaa) -- don't know why delay would be used here. end end function draw() background(40, 40, 50) if b==nil then b=tween.delay(3,bbb) end end function aaa() print("delay from setup() 3 seconds.") a=nil end function bbb() print("delay from draw() 3 seconds.") b=nil end function ccc() print("delay from touch() 3 seconds.") c=nil end function touched(t) if t.state==BEGAN then c=tween.delay(3,ccc) -- a delay might be useful here end end
@dave1707 I am running your code. That’s not my method, though. I never just said to plonk tween.delay in draw without a timer.
You’re telling me code I never posted is wrong.
@SkyTheCoder OK, I agree with you there. You never said to use tween.delay() in draw(), that was a mistake on my part. My whole intent was to show newbies that tween.delay(), as you had it coded in setup(), can’t be used as a delay anywhere in a program. Some places it works as intended, some places it won’t.
@dave1707 I get a little intense sometimes… Sorry about that.
@SkyTheCoder No need to say you’re sorry. I kind of enjoy our discussions, it gets me thinking more.