Creating a Flash spring
Think about how a spring works in the real world. You can pull the two ends apart and when you let go, they’ll spring together. The further apart you pull the ends, the faster the spring effect. When you pull a spring, the rebound causes it to bounce back in the opposite direction, not quite as far as the original length. This effect repeats until the spring settles back into its original position.
We’ll create this effect in Flash. Start by opening the resource file spring.fla. You'll see a ball on the Stage. The ball symbol has a centre registration point to make calculations easier. If you hide the ball layer, you’ll see that the layer below it has a square shape. This shows the tether position for the spring.
We need to set up the variables first. Select the ball instance and open the Actions panel with the F9 shortcut key. Enter the code below.
onClipEvent (load) {
var anchorX = 198.4;
var anchorY = 130.5;
var inertia = 0.9;
var springBack = 0.2;
var moveToX;
var moveToY;
var newXPos = 0;
var newYPos = 0;
var haveMoved = false;
}
The first two variables set the co-ordinates of the anchor point. This represents the x and y position of the movie clip on the square layer. The next variable, inertia deals with how far the movie clip springs back. I've set the value to 0.9. I need to choose a value less than 0.9 so that the instance won't spring back to its previous position. If I used a value of 1, the ball would spring back forever. The springBack variable tells me how fast the instance will spring back towards the anchor point. Using a higher value will result in a quicker response from the spring.