Drivable Car
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.
- //www.swfspot.com
- //movement speed of the car
- var speed=7;
- //Compute pi/180. The value is used twice for each frame, so storing it as a variable saves CPU
- var piOver180=Math.PI/180;
- onEnterFrame=function(){
- //If the left or right keys are down, rotate the wheels
- if(Key.isDown(Key.LEFT)){
- car.leftwheel._rotation=Math.min(24,Math.max(-24,car.leftwheel._rotation-5));
- car.rightwheel._rotation=Math.min(24,Math.max(-24,car.rightwheel._rotation-5));
- }
- if(Key.isDown(Key.RIGHT)){
- car.leftwheel._rotation=Math.min(24,Math.max(-24,car.leftwheel._rotation+4));
- car.rightwheel._rotation=Math.min(24,Math.max(-24,car.rightwheel._rotation+4));
- }
- //check if the up or down keys are pressed
- if(Key.isDown(Key.UP)||Key.isDown(Key.DOWN)){
- //if the up key is down, the car moves forward
- if(Key.isDown(Key.UP)){
- carDirection=1;
- //otherwise move the car backwards
- }else{carDirection=-1;}
- //default turning to false
- turning=0;
- //if the left or right keys are pressed, set turning to true and rotate the car
- if(Key.isDown(Key.LEFT)){
- turning=1;
- car._rotation-=5*carDirection;
- }
- else if(Key.isDown(Key.RIGHT)){
- turning=1;
- car._rotation+=5*carDirection;
- }
- //if the car isn't turning, reset the rotation of the tires
- if(!turning){
- car.leftwheel._rotation=0;
- car.rightwheel._rotation=0;
- }
- //move the car. The y coordinate plane is flipped in flash, so we use - instead of +
- car._x+=Math.sin(car._rotation*piOver180)*speed*carDirection;
- car._y-=Math.cos(car._rotation*piOver180)*(speed)*carDirection;
- }
- }
Download Source File
Comments Currently Disabled


