Acceleration, Friction, and Wrapping
Employing Acceleration
Now let's get that "5" out of the conditional statements. We'll start by referencing a new variable we'll call yAccel:
Now we need to declare the variable.... I'm going to slow thing down a bit and change from "5" to "2":
Let's test to make sure we haven't broke anything:
OK, what's really going on?
This line is part of the onEnterFrame function, and is constantly running:
ship._y+=ySpeed;
When ySpeed=0; the ship stays in the same position. Now let's tap the UP key once, and this block of code springs into action:
if(Key.isDown(Key.UP)){
ySpeed-=yAccel;
}
0-2 = -2. The variable ySpeed is -2. Now we're constantly adding -2 to ship._y. The ship moves up 2 pixels, 24 times per second.
Tap UP again. -2+-2 = -4. The variable ySpeed is -4. The ship moves up 4 pixels, 24 times per second.
Now tap DOWN once. This line of code springs into action:
if(Key.isDown(Key.DOWN)){
ySpeed+=yAccel;
}
-4+2 = -2. The variable ySpeed is -2. The ship moves up 2 pixels, 24 times per second.