Best viewed in Firefox

Awesty Productions

Realistic Car Movement

April 20th, 2007 by awesty

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.

Making the wheel

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’.

Making the 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.

Now if you test your movie (Ctrl+Enter) you should have something like this (link).

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • del.icio.us
  • digg
  • Furl
  • MyShare
  • NewsVine
  • Netscape
  • Reddit
  • Simpy
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

RSS feed | Trackback URI

15 Comments »

Comment by Hansa
2007-04-20 23:15:05

have nothing else to say then first comment :D

 
Comment by chris h Subscribed to comments via email
2007-04-21 15:24:02

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?

Comment by awesty
2007-04-21 19:59:24

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/

 
 
Comment by Daniel Subscribed to comments via email
2007-04-24 01:55:26

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 :D

Comment by awesty
2007-04-24 20:11:03

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.

 
 
Comment by Timmy Subscribed to comments via email
2007-04-26 07:57:39

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?

Comment by awesty
2007-05-01 20:36:49

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.

 
 
Comment by Kurt Subscribed to comments via email
2007-04-28 23:30:31

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

Comment by awesty
2007-05-01 20:34:35

:D

 
 
Comment by eblup Subscribed to comments via email
2007-05-04 13:35:29

hey put in ground and make it ride the line. plz

 
Comment by Timmy Subscribed to comments via email
2007-05-15 09:25:41

how could i make it go up and down not just right and left?
i don’t care about the wheels

Comment by Timmy Subscribed to comments via email
2007-05-15 09:28:53

oh and what does the registration do?(the little boxs that u could click on when you are converting to symbol)???

 
 
Comment by Trunk Monkey
2007-06-04 18:40:21

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

Comment by awesty
2007-07-02 18:29:25

I dont really understand what you are trying to say.

 
 
Comment by simon Subscribed to comments via email
2007-08-10 19:32:37

how do i make it go faster??

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.