Discuss Scratch
- FF76YT
-
Scratcher
13 posts
New Coding Block [glide block with steps]
Is there any way to do the same thing except turn a certain amount of degrees?
- FF76YT
-
Scratcher
13 posts
New Coding Block [glide block with steps]
Is there any way to do the same thing except turn a certain amount of degrees?
Thanks for helping me out, but can you do the same things but turning a certain amount of degrees?Thanks!There's not really a way to do that, unfortunately. But once you have the “define” script in your backpack, you can always drag it out whenever you want.
One more thing, can you add this block into scratch so I don't always have to type this in?
- _nix
-
Scratcher
1000+ posts
New Coding Block [glide block with steps]
Is there any way to do the same thing except turn a certain amount of degrees?Turning is a little bit more complicated, but here's the basic idea: The way this works is, you want to turn a certain amount (we'll call this the rotation) over a certain amount of time. This means we sort of want to make little steps. You know how moving 10 steps 10 times makes the sprite move 100 steps in total? Well, we want to rotate a certain amount a certain number of times, and that should result in us turning that amount of rotation we wanted.
Here's a script, which I'll explain after you take a look at it:
define turn (rotation) degrees in (time) seconds
repeat ((time) * (30))
turn cw ((rotation) / ((time) * (30))) degrees
end
Okay, so, there's two important things here. The first one is that we use a “repeat” block – this is what makes the rotation look animated. The other part is that we use a “divided by” block – that's the slash sign. (I'm not sure how advanced you are in math, but in school, the division sign looks like “÷”. In Scratch, and most programming languages, it's a “/” instead.) The division block is actually probably the most important block in all of animation – it's what lets you say "do part of the animation at a time“. We have to understand one other thing before we see how we use ”/“, though:
You might have noticed that we used ”time * 30“ a couple times. The important thing to understand here is that Scratch projects run at 30 frames per second. That means the ”turn degrees“ block (and anything else in the loop) will be run once every 30th of a second. We say that every time it runs is a frame. So the question is this: How many frames long is the animation? Well, that's pretty simple - we just have to multiply the number of seconds our animation lasts, ”time“, and the number of frames in a second, 30

So we put ”time * 30“ into our ”repeat" block, so that the animation lasts the right number of frames. Now the trick is – if we want to turn a total of “rotation” degrees, how many degrees do we have to turn each frame? Well, this is pretty simple. We just divide “rotation” by the number of frames in our animation, which is “time * 30”.
I think it'll be clearer just how this works if I give you an example. Let's take a look at an example usage of our block:
turn (120) degrees in (2) seconds :: custom // This means "rotation" is 120 and "time" is 2.
Let's do that first step and figure out how many frames will be in our animation. Well, there's 30 frames in a second, and 2 seconds, so the animation of the sprite turning will last 30 * 2 = 60 frames. Now we need to figure out how many degrees the sprite will turn in each frame. That's “rotation / (time * 30)”, or rotation divided by the number of frames in the animation. That's 120 divided by 60, which is pretty easy to do: 120 / 60 = 2

So basically, if we replace the blocks with the values we calculated just now, we've ended up doing this script:
repeat (60)
turn cw (2) degrees
end
And it's pretty easy to verify that we're doing the right thing here. If we turn 2 degrees 60 times, we'll obviously turn a total of 120 degrees, which is what we want. And if we do the turning across 60 frames, it'll last 2 seconds (because there are 30 frames in 1 second), which is also what we want.
By the way, in case you were wondering, we could do this to make all sorts of other glide-y blocks, too. For example, here's one that changes the X position by a certain amount over a certain number of seconds:
define change x by (dist) in (time) seconds
repeat ((time) * (30))
change x by ((dist) / ((time) * (30))
end
And here's one that does the same but for the sound volume:
define change volume by (amount) in (time) seconds
repeat ((time) * (30))
change volume by ((amount) / ((time) * (30))
end
You could use that to make a song fade in:
set volume to (0) %
change volume by (100) in (2) seconds :: custom
change volume by (-100) in (2) seconds :: custom // ...Or out!
But I'm getting sidetracked. I just like showing the various things you can do with animating stuff like this
The basic idea is that you want to do something over a certain number of frames, and that works for doing just about anything, really.Hopefully this helps! I think these scripts are a bit easier to understand than the crazy “glide (time) seconds (dist) steps” block I made earlier, but feel free to ask anything if you're wondering or confused!

- ACE009
-
Scratcher
100+ posts
New Coding Block [glide block with steps]
Even with the original idea, what would happen when the sprite turns during movement? Does the sprite finish it’s path, or does it turn, then keep on going? Thus, it’s better to just make a custom block that does what you want.
Last edited by ACE009 (May 2, 2018 11:16:15)


