Welcome to the ultimate velocity tutorial. This tutorial will tell you what velocity is, how it works (how to do it), and why it works.
1. What is velocity?
2. How do you do it?
3. Why does it work?
4. Good points made by other people
You can see an example of what this script does here.
Last edited by PullJosh (2012-06-13 16:29:18)
Offline
How do you do it?
I'm not going to explain the script right now. Explaining goes in the next part. Here's the script:
when gf clicked set [velocity v] to (0) goto x:(0) y:(0) forever if <touching color [#000000]?> // replace this with the ground color. change y by (2) // we could do velocity for this part too, but I'm not going to. else change y by (-2) end if <key [right arrow v] pressed?> change [velocity v] by (0.2) end if <key [left arrow v] pressed?> change [velocity v] by (-0.2) end if <not<(velocity) = [0]>> if <(velcocity) > [0]> change [velocity v] by (-0.1) else change [velocity v] by (0.1) end end if <touching [edge v]?> set [velocity v] to ((velocity) * (-1)) end change x by (velocity)Don't understand the orange blocks? Go here for a tutorial on them.
Last edited by PullJosh (2012-06-13 16:11:38)
Offline
Why does it work?
First of all, let's remember what velocity is: a number that changes to make things smoother. That means that this variable (named "velocity" in this script) changes, therefore changing the amount that the sprite moves. We start out with this:
when gf clicked set [velocity v] to (0) goto x:(0) y:(0)This part of the script just resets the velocity. That way, if you were going left when you clicked the green flag, you wouldn't keep going left at the start.
if <(velocity) > (0)> change [velocity v] by (-0.1) else change [velocity v] by (0.1) endIf the velocity is grater than (bigger than) zero, it will go right. This is why: Remember what velocity is: an amount that the sprite moves. If you change the sprite's x position by 5, it will move five steps the the right. Try it out with a simple script, if you want. The same goes for left. If it is a negative number, it will go left. That means that we need to separate scripts. If it is going right we need to make it closer to going left, and vise-versa. That is what this part of the script does.
set [velocity v] to ((velocity) * (-1))Whenever you multiply a number by negative one it reverses. This is what I mean: 1 * -1 = -1, -3 * -1 = 3, 5 * -1 = -5, etc. This then changes if it is going left or right (see above paragraph).
Last edited by PullJosh (2012-06-12 17:24:50)
Offline