Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do I make a sprite change size (or do other things) in a specific amount of time?
- Cheezzychiknzzz
-
Scratcher
500+ posts
How do I make a sprite change size (or do other things) in a specific amount of time?
Suppose I want to make a sprite start at 100%, then shrink it smoothly to 80% in 2 seconds, and then I want it grow back smoothly to 100% in 2 seconds (again) and repeat the cycle forever. Plus, how do I apply it to other similar properties such as direction, graphic effects, etc…?
- ajsya
-
Scratcher
1000+ posts
How do I make a sprite change size (or do other things) in a specific amount of time?
I would just create some kind of loop that made it work out so that the sprite changed by the right size in the right amount of time.
I was going to try creating a custom block that did the changing size part, but I ran out of time. I think the timing is also a little messed up for some Scratch technical reason. It takes over 2 seconds to change the size even though it should only take 2 seconds theoretically since .1*20 is 2. I tried playing around with “run without screen refresh” and turbo mode, but it wasn't working for me.
when green flag clicked
forever
repeat (20)
change size by (-1)
wait (.1) secs
end
repeat (20)
change size by (1)
wait (.1) secs
end
end
I was going to try creating a custom block that did the changing size part, but I ran out of time. I think the timing is also a little messed up for some Scratch technical reason. It takes over 2 seconds to change the size even though it should only take 2 seconds theoretically since .1*20 is 2. I tried playing around with “run without screen refresh” and turbo mode, but it wasn't working for me.
- dem_bot
-
Scratcher
1000+ posts
How do I make a sprite change size (or do other things) in a specific amount of time?
You're probably looking for a sine wave. A sine wave usually goes from -1 to 1, so we have to multiply it by 10 to go from -10 to 10. Then we add 90 to it to go from 80 to 100.
Edit: i just realised u want it to loop in 4 seconds.
In that case, the really big number is 360/4 * 3600 * 24
((([sin v] of (t)::operators) * [10]) + [90])t should constantly increase. Usually I would use the days since 2000 for this:
((days since 2000) * [big number just try some stuff out til u have a nice speed])
Edit: i just realised u want it to loop in 4 seconds.
In that case, the really big number is 360/4 * 3600 * 24
Last edited by dem_bot (Yesterday 16:59:25)
- ajsya
-
Scratcher
1000+ posts
How do I make a sprite change size (or do other things) in a specific amount of time?
You're probably looking for a sine wave. A sine wave usually goes from -1 to 1, so we have to multiply it by 10 to go from -10 to 10. Then we add 90 to it to go from 80 to 100.Smart! This is way better than what I came up with.((([sin v] of (t)::operators) * [10]) + [90])t should constantly increase. Usually I would use the days since 2000 for this:((days since 2000) * [big number just try some stuff out til u have a nice speed])
Edit: i just realised u want it to loop in 4 seconds.
In that case, the really big number is 360/4 * 3600 * 24
- Woodfur
-
Scratcher
100+ posts
How do I make a sprite change size (or do other things) in a specific amount of time?
I think the timing is also a little messed up for some Scratch technical reason. It takes over 2 seconds to change the size even though it should only take 2 seconds theoretically since .1*20 is 2. I tried playing around with “run without screen refresh” and turbo mode, but it wasn't working for me.
Loop blocks always add a 1/30th-second wait at the end. That's the frame rate Scratch projects run at, so it's better to work with it than against it.
forever
repeat (60) //number of frames in 2 secs
change size by ((1) / (3))
end
repeat (60)
change size by ((-1) / (3))
end
Run without screen refresh will cancel the delay, but it's meant for calculations that happen instantaneously—using it on actions that take measurable time can break your project.
- Cheezzychiknzzz
-
Scratcher
500+ posts
How do I make a sprite change size (or do other things) in a specific amount of time?
But what if I want it to grow or shrink in a fractional number of time?
- dem_bot
-
Scratcher
1000+ posts
How do I make a sprite change size (or do other things) in a specific amount of time?
But what if I want it to grow or shrink in a fractional number of time?with the sine wave you can basically do whatever as long as it fits in a double (the variable type scratch uses for numbers).
31104000/amount of seconds per rotation
- kemlevor
-
Scratcher
96 posts
How do I make a sprite change size (or do other things) in a specific amount of time?
when green flag clicked
set [caracteristic time v] to [2]
set [time v] to [0]
set [t0 v] to [150]
set [tf v] to [100]
set [step v] to [0]
forever
if <(step) = [0]> then
change [time v] by (timer)
if <(time) > (caracteristic time)> then
set [time v] to (caracteristic time)
end
set size to ((t0) + (((tf) - (t0)) * (( ([e^ v] of (((5) * (time)) / (caracteristic time))::operators) - (1)) / (([e^ v] of (5)::operators) - (1))))) %
if <(time) > (caracteristic time)> then
set [time v] to [0]
set [step v] to [1]
end
else
change [time v] by (timer)
if <(time) > (caracteristic time)> then
set [time v] to (caracteristic time)
end
set size to ((tf) + (((t0) - (tf)) * (( ([e^ v] of (((1) * (time)) / (caracteristic time))::operators) - (1)) / (([e^ v] of (1)::operators) - (1))))) %
if <(time) > (caracteristic time)> then
set [time v] to [0]
set [step v] to [0]
end
- Discussion Forums
- » Help with Scripts
-
» How do I make a sprite change size (or do other things) in a specific amount of time?