Newest Articles

8 Ball Pool
Photo Reel
Image Color Tinting using Actionscript
3d Rotating Image Cube
Image Slider with Easing
Catapult Game


Popular Articles

True Fullscreen Flash Mode
8 Ball Pool
FLV Player
Image Slider
Mp3 Player with XML Playlist
Album Slide


Random Articles

Space Shooter Game
Alert Box
Krypto Movie Quotes
Falling Snowflakes
Mouse Following Preloader
Horizontal Line Transition


Links

Foundation-Flash
Tutorial4Me
MickM
TutorialQuest
Tutorialsphere.com - Free Online Tutorials
Newgrounds
TWiT
Link to SwfSpot
Swf Spot



rss feed

Drivable Car

Drivable Car
AddThis Social Bookmark Button
Description: This car moves forwards, backwards, turns, and has angling wheels.
Author: John Bezanis
Added: January 28th 2007
Version: Flash 8
Total Views: 5940
Views in the Past 7 Days: 82


The car is drivable on the press of the direction keys. The car moves forward, backwards, turns, and has moving front wheels.
  1. //www.swfspot.com
  2.  
  3. //movement speed of the car
  4. var speed=7;
  5. //Compute pi/180. The value is used twice for each frame, so storing it as a variable saves CPU
  6. var piOver180=Math.PI/180;
  7. onEnterFrame=function(){
  8.   //If the left or right keys are down, rotate the wheels
  9.   if(Key.isDown(Key.LEFT)){
  10.     car.leftwheel._rotation=Math.min(24,Math.max(-24,car.leftwheel._rotation-5));
  11.     car.rightwheel._rotation=Math.min(24,Math.max(-24,car.rightwheel._rotation-5));
  12.   }
  13.   if(Key.isDown(Key.RIGHT)){
  14.     car.leftwheel._rotation=Math.min(24,Math.max(-24,car.leftwheel._rotation+4));
  15.     car.rightwheel._rotation=Math.min(24,Math.max(-24,car.rightwheel._rotation+4));
  16.   }
  17.   //check if the up or down keys are pressed
  18.   if(Key.isDown(Key.UP)||Key.isDown(Key.DOWN)){
  19.     //if the up key is down, the car moves forward
  20.     if(Key.isDown(Key.UP)){
  21.       carDirection=1;
  22.     //otherwise move the car backwards
  23.     }else{carDirection=-1;}
  24.     //default turning to false
  25.     turning=0;
  26.     //if the left or right keys are pressed, set turning to true and rotate the car
  27.     if(Key.isDown(Key.LEFT)){
  28.       turning=1;
  29.       car._rotation-=5*carDirection;
  30.     }
  31.     else if(Key.isDown(Key.RIGHT)){
  32.       turning=1;
  33.       car._rotation+=5*carDirection;
  34.     }
  35.     //if the car isn't turning, reset the rotation of the tires
  36.     if(!turning){
  37.       car.leftwheel._rotation=0;
  38.       car.rightwheel._rotation=0;
  39.     }
  40.     //move the car. The y coordinate plane is flipped in flash, so we use - instead of +
  41.     car._x+=Math.sin(car._rotation*piOver180)*speed*carDirection;
  42.     car._y-=Math.cos(car._rotation*piOver180)*(speed)*carDirection;
  43.   }
  44. }

Download Source File
Questions and Suggestions
Good tut, but you could add physics, like friction, drag, etc. This would cause the car to behave more realistically. It would coast, slide, and other good stuff.
June 2nd 2007 11:06AM   -   Egan
can utell how thr car was made
October 11th 2007 22:10PM   -   altaf
Add a Question or Suggestion
name:
website (optional):
captcha type the characters into the box
message (5000 characters or less):