- Home
- Tutorials
- Flash
- Intermediate
- Car Movement
Car Movement

Wheel Trails
Philip Peralov
Philip is a Flash guru and a ActionScript freak. He has done a couple of projects, featuring games, programs, websites & cartoons. He's only 15 years old, but he well knows programing in Java & ActionScript and is a professional in Animating. In his free time he goes to the local High School :) in Bitola, Macedonia, Europe, where he lives.
Now, we'll program the car, so it can left a trail on the ground, made by the wheels. For that purpose we'll use the Drawing API. The drawing API allows you to draw lines and shapes with ActionScript and gives you the freedom to start an application with the computer equivalent of a blank canvas, on which you can create whatever images you wish. We need to know what we want to do with the drawing API.
In this case we want to make a wheel trail. So, we're going to use the car _x and _y properties to draw those lines. First we'll try to draw just one line from the center of the car. So let's start.
All the scripts goes to the car mc (short term for MovieClip). Create a mc for the drawing (we use mc instead the stage to draw, because like this we can change the depth property).
The new empty mc it's called "drawingMC" and it has a depth value of 1. We use swapDepths to make the car over the trail. Otherwise the car can (will) be under them.
Now under the enterFrame event add the following script:
Yeah, it works nice, but at the start the lines start from 0,0 coordinates. Fix that by applying this script under the onLoad event.
Next, we need to move the trail to the left or the right so it will looks like it's coming from one of the tires. We can't just use _x+2 or something like that, because the car rotates and the values need to change. That's why under the enterFrame event enter:
I'm not going to explain this script, because we already discussed this on page 2.
However, we're not going to remove the recently script _root.drawing.moveTo (_x,_y); and we're going to add something similar to the enterFrame event because when we gonna add another trail those two will get together.
Modify the script to something like this.
All we need to do now is to add the following script to the onLoad event and we're done.
We applied the same script to the onLoad event because otherwise the lines will begin with coordinates of 0, 0, and we sure don't want that :) And if you want to apply stronger trail when the car brakes, think a litlle yourself......It's easy.
The source file and this tutorial in .pdf are in attachments
In this case we want to make a wheel trail. So, we're going to use the car _x and _y properties to draw those lines. First we'll try to draw just one line from the center of the car. So let's start.
All the scripts goes to the car mc (short term for MovieClip). Create a mc for the drawing (we use mc instead the stage to draw, because like this we can change the depth property).
onClipEvent (load) {
var speed:Number = new Number (0);
_root.createEmptyMovieClip ("drawing",1);
this.swapDepths (_root.drawing);
}
The new empty mc it's called "drawingMC" and it has a depth value of 1. We use swapDepths to make the car over the trail. Otherwise the car can (will) be under them.
Now under the enterFrame event add the following script:
_root.drawing.lineStyle (3,0xeeeeee,100);
_root.drawing.lineTo (_x,_y);
Yeah, it works nice, but at the start the lines start from 0,0 coordinates. Fix that by applying this script under the onLoad event.
_root.drawing.moveTo (_x,_y);
Next, we need to move the trail to the left or the right so it will looks like it's coming from one of the tires. We can't just use _x+2 or something like that, because the car rotates and the values need to change. That's why under the enterFrame event enter:
xTrail = Math.sin (_rotation * Math.PI / 180) * 10;
yTrail = Math.cos (_rotation * Math.PI / 180) * 10;
xTrailCalculated = _x - yTrail;
yTrailCalculated = _y - xTrail;
I'm not going to explain this script, because we already discussed this on page 2.
However, we're not going to remove the recently script _root.drawing.moveTo (_x,_y); and we're going to add something similar to the enterFrame event because when we gonna add another trail those two will get together.
Modify the script to something like this.
xTrailCalculated = _x - yTrail;
yTrailCalculated = _y - xTrail;
xTrailCalculated2 = _x + yTrail;
yTrailCalculated2 = _y + xTrail;
_root.drawing.moveTo (xTrailCalculated,yTrailCalculated);
_root.drawing.lineTo (_x - yTrail,<span id="_de_spell_word_1">_y</span> - xTrail);
_root.drawing.moveTo (xTrailCalculated2,yTrailCalculated2);
_root.drawing.lineTo (_x + yTrail,_y + xTrail);
All we need to do now is to add the following script to the onLoad event and we're done.
xTrail = Math.sin (_rotation * Math.PI / 180) * 10;
yTrail = Math.cos (_rotation * Math.PI / 180) * 10;
xTrailCalculated = _x - yTrail;
yTrailCalculated = _y - xTrail;
xTrailCalculated2 = _x + yTrail;
yTrailCalculated2 = _y + xTrail;
We applied the same script to the onLoad event because otherwise the lines will begin with coordinates of 0, 0, and we sure don't want that :) And if you want to apply stronger trail when the car brakes, think a litlle yourself......It's easy.
The source file and this tutorial in .pdf are in attachments
Spread The Word
Attachments
17 Responses to "Car Movement" 
|
said this on 09 Aug 2007 6:06:16 PM CST
Can't see the wheel trail code ... i.e. "See the next page for the wheel trail." doesn't exist?
|
|
said this on 11 Aug 2007 1:34:28 PM CST
It wasn't finished yet. Now you can see it.
|
|
said this on 14 Aug 2007 8:13:40 AM CST
Good job, the car movement was excellent. but the fla file is not opening in MX version.
|
|
said this on 14 Aug 2007 10:01:32 PM CST
awesome work. this script will serve me for many uses as I mod it. love the fact I it offers so much for so little file size
|
|
said this on 15 Aug 2007 7:35:17 AM CST
hey dude, your good! i like your script it helps me alot and give me an idea to make more....tnx you rulles!!!!
|
|
said this on 18 Aug 2007 8:47:52 PM CST
great tutorial, its the first time i did something like this so i was very happy when i finished it. But i had an idea if u just put a sound file on loop on each layer would it play while you drove around?
|
|
said this on 20 Aug 2007 12:29:04 AM CST
nice work & nice script. keep it up.
|
|
said this on 21 Aug 2007 10:08:41 AM CST
Whilst this is a good tutorial, it would be useful if the source file attachment contained the exact same code as the tutorial itself. This is not a good way to teach others to learn especially when the source file needs to be referenced.
|
|
said this on 23 Aug 2007 6:58:26 AM CST
Agree with Ian - nice tutorial, but disappointing that the code in you .fla is different to that in the tutorial. I had some problems and couldn't use the .fla code to resolve them.
|
|
said this on 25 Aug 2007 3:31:42 PM CST
Sorry bout the .fla I'll fix that. I guess that was a prev test of the tutorial.
|
|
said this on 22 Sep 2007 10:22:48 AM CST
your script is very used for beginner .And Excellent understandable by other also,
thank u . |
|
said this on 26 Sep 2007 7:11:37 AM CST
Re your action script car game...
My 10 year old son keeps asking me to help him build some Flash computer games. I barely know flash, nor action script, yet here you have given me chance to learn with him. He will be blown away with this action script he comes home from school tonight. Thankyou Philip |
|
said this on 26 Sep 2007 4:20:38 PM CST
Great tutorial. However I can't seem to find the source .fla on these pages. I want to trouble shoot why my tire trails are running side ways and not parallel with the direction of the car.
|
|
said this on 16 Oct 2007 5:32:01 PM CST
i could not get the tires to skid because of the line with the <span in it. once i took that out i was able to get one tire to skid, but not them both. it said that your script was wrong.
|
|
said this on 03 Nov 2007 9:46:48 AM CST
@rich
you'll go to the last page and there you'll find the files in attachments. BTW great tut philip |
|
said this on 21 Nov 2007 4:56:35 PM CST
Philip, great tutorial, although, not quite flawless in my opinion. I can't accelerate at all. Will ask on forums. I was also wondering why did you use control as the stop, why not space?
|
|
said this on 14 Dec 2007 7:04:58 PM CST
Hey, great tutorial, I am trying to have this type actionscript for one part of my site, but when I go to all of the other areas the car is still there. I have tried stop(); and some other things with the frames and nothing is working. Just curious of how to fade it out and not use it on the whole site.
|


Author/Admin)