Newest Articles

MegaCombs
Flash Media Player
XML Driven Pie Chart
Base Defender
Hangman Game
8 Ball Pool


Popular Articles

True Fullscreen Flash Mode
Mp3 Player with XML Playlist
FLV Player
Create Pong
8 Ball Pool
Base Defender


Random Articles

Motion along a Path
Image Slider with Easing
Falling Snowflakes
Growing Tree using Recursion
MegaCombs
Image Slider


Links

Shapes the Game
Reddit
Newgrounds
TWiT
Link to SwfSpot
Swf Spot

Contact me on Google+



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


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
Comments Currently Disabled