.

Page 1

In this tutorial you will learn how to make a cars wheels spin when it moves, and for it to speed up and slow down as it moves. I am using flash 8, but Flash MX 2004 will work as well.

First of all you will need to draw your cars wheel. Once you have done that select it and make it a MC (Movie Clip). Make sure the registration point is in the center.



Now, copy and paste the wheel so your car will have two wheels. Give one of them an instance name of 'wheel1? and the other an instance name of ' 'wheel2?. Now draw the body of the car and make the whole thing a movieclip. Give this MC an instance name of 'car'.



So now you should have your car MC, which contains your two wheel MCs, wheel1 and wheel2, as well as the rest of the car. Now paste this code onto the first frame.
var speed:Number = 0;
var slow:Number = 0.90;
var maxSpeed:Number = -10;
onEnterFrame = function () {
    if (Key.isDown(Key.RIGHT)) {
        speed += 1;
    } else if (Key.isDown(Key.LEFT)) {
        speed -= 1;
    } else {
        speed *= slow;
    }
    if ((speed<0.5 && speed>0) || (speed>-0.5 && speed<0)) {
        speed = 0;
    }
    _root.car._x += speed;
    for (i=1; i<3; i++) {
        _root.car["wheel"+i]._rotation += speed;
    }
    if(speed < maxSpeed){
        speed = maxSpeed;
    }
    if(speed > Math.abs(maxSpeed)){
        speed = Math.abs(maxSpeed);
    }
};
Now to explain the code.

Lines 1-3 are declaring some variables. The first one, speed, well tell the car how fast to move and tell the wheels how fast to spin. The second one, slow, is the rate the car will slow down at when no buttons are pressed. If it is 1, the car will not slow down. If it is above one the car will speed up instead of slowing down. If is it 0 the car will slow down immediately. So the faster you want your car to slow down, the lower you have the number.
The third variable declared is the maximum speed the car can move. It is set to 10 pixels a frame at the moment. You might be wondering why it is a negative number but I will explain that later.

Line 4 as always means 'Every time this frame is entered do the following'. So if your frame rate is 12, the code will run 12 times a second.

Lines 5-11 are what makes the car speed up and slow down. If the right arrow is pressed down the speed of the car will increase by 1. If the left arrow key is pressed down the speed of the car will decrease by 1. If the speed is in the negatives (eg -4) the car will move backwards. If none of the buttons are pressed the speed variable is being multiplied by the slow variable we declared so it loses speed.

Lines 12-14 is telling flash that is the car is moving slower than 0.5 pixels than to stop it moving. Otherwise the car will never stop, and will end up moving a speed like 0.0000001.

Line 15 is what makes the car move. It tells flash to make the car move as fast as the variable speed. So if speed is equal to 3, the car will move 3 pixels.

Lines 16-18
are making the wheels spin. There is a for statement there, so you can just write wheel+i instead of wheel1 and wheel2.

Lines 19-21 are stopping the car from moving a speed more than the max speed backwards, which is -10.

Lines 22-24 are stopping the car from moving a speed more than the max speed forwards. Since the max speed is -10, that wouldn't work so I added in a little maths function. Math.abs() converts a negative number to a positive number.



No comments found ! Click here to be the first adding comments for this tutorial!