Realistic Car Movement
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.
You can see an example of what we will be making HERE.
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 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.
Now if you test your movie (Ctrl+Enter) you should have something like this (link).















have nothing else to say then first comment
hey well umm can u make an example link? thank u :] and do u know how to make time delay on a button? like ok i want a button on a screen that u cant click for 30 seconds and i want a little timer right next to the button. like it says (30)(29)(28)(27)(26)….and so on. like it counts down. can u not email me and just add it as a comment. is there a way i can show u my game i ahve so far?
http://www.awestyproductions.com/images/car_tut.swf
That is an example of the tutorial.
About the countdown thing, try this tutorial:
http://www.awestyproductions.c.....tinterval/
wow really helped but even though i have this code i still dont get how to make ramps and gravity for the car…
Well if its no to hard for ya please make a tut
Well that is getting a bit more advanced. Gravity I could do, but when it come to making it get air off I ramp I couldn’t.
how could u make the car wrap around to the other side like if the car goes off the right side of the stage, it appears on the left side?
Well the easiest way would be to do something like:
if(this._x > (stage.width+this._width)){
this._x = (0-this._width);
}
That would only be for the right side of the stage though. There are other and better ways of doing it but that is the easiest IMO.
holly smokes! the last time i was on this website it was alot different!
Lol i gotten back into runescape and now I’m getting back into flash
hey put in ground and make it ride the line. plz
how could i make it go up and down not just right and left?
i don’t care about the wheels
oh and what does the registration do?(the little boxs that u could click on when you are converting to symbol)???
I want the car to come in from the side but not the person controlling it. I used this code to move in the side (which I got off the gravity tut but it changed abit of it)
onClipEvent (load) {
acc = 9.8;
time = 0;
fps = 20;
}
onClipEvent (enterFrame) {
time = time+1/fps;
this._x -= Math.pow(time, 2)*acc;
_root.restart.onPress = function() {
_root.aircraft._x = -50;
time = 0;
};
}
but when I do it the wheels
don’t move
I dont really understand what you are trying to say.
how do i make it go faster??