1:  package
   2:  {
   3:      import flash.events.Event;
   4:      import flash.display.MovieClip;    
   5:   
   6:      public class Car extends MovieClip
   7:      {
   8:          // Internal properties
   9:          public var PositionX:Number = 300;
  10:          public var PositionY:Number = 200;
  11:          public var Speed:Number = 0;
  12:          public var Direction:int = 0; // 0 up, 90 right, 180 down, -90 left
  13:          public var TurningSpeed:Number = 0;
  14:          
  15:          public var HandBrakeOn:Boolean = false;
  16:          public var GasOn:Boolean = false;
  17:          public var BrakeOn:Boolean = false;
  18:          public var TurnLeft:Boolean = false;
  19:          public var TurnRight:Boolean = false;
  20:          
  21:          // Car characteristics
  22:          const BRAKE_FORCE:Number = .9; // Speed reduction per tick
  23:          const ACCERATION_FORCE:Number = .9; // Speed increase per tick
  24:          const TURNING_FORCE:Number = 4.5; // Turn amount per tick
  25:          
  26:          const FRICTION:Number = 1; // Slowdown per tick
  27:          
  28:          const MAX_SPEED:Number = 18; // Max car speed
  29:          const MAX_TURN_SPEED:Number = 10; // Max turning speed
  30:          
  31:          public function Car():void
  32:          {
  33:              trace("Car created" );
  34:              addEventListener( Event.ENTER_FRAME, enterFrame );
  35:          }
  36:          
  37:          // Tick
  38:          function enterFrame( ev:Event ):void
  39:          {
  40:              // Accelerator
  41:              if ( GasOn && Speed <= MAX_SPEED )
  42:                  Speed += ACCERATION_FORCE; // Gas on
  43:              else if ( !GasOn && Speed > 0 )
  44:                  Speed -= FRICTION; // Gas on, friction slow down
  45:                  
  46:              // Brake
  47:              if ( BrakeOn && Speed > 0 )
  48:                  Speed -= BRAKE_FORCE;
  49:   
  50:              // Make sure speed doesn't become a negative number (No revesing - yet)
  51:              if ( Speed < 0 )
  52:                  Speed = 0;                                
  53:          
  54:              // Turning
  55:              if ( TurnRight && TurningSpeed <= MAX_TURN_SPEED)
  56:              { // Right
  57:                  TurningSpeed += TURNING_FORCE;
  58:              }
  59:              else if ( TurnLeft && (TurningSpeed*-1) <= MAX_TURN_SPEED )
  60:              { // Left
  61:                  TurningSpeed -= TURNING_FORCE;
  62:              }
  63:              else
  64:              { // Reset steering
  65:              
  66:                  // If only a little out, reset completely
  67:                  if ( TurningSpeed < .5 && TurningSpeed > -.5 )
  68:                      TurningSpeed = 0;
  69:                  
  70:                  // Slowly reset
  71:                  if ( TurningSpeed > 0 )
  72:                      TurningSpeed -= TURNING_FORCE;
  73:                  else if ( TurningSpeed < 0 )
  74:                      TurningSpeed += TURNING_FORCE;
  75:              }
  76:              
  77:              // Change direction - but only if moving!
  78:              if ( Speed > 0 )
  79:              {
  80:                  Direction += TurningSpeed;
  81:                  if ( Direction > 360 )
  82:                      Direction = 0;
  83:                  else if ( Direction < 0 )
  84:                      Direction = 359;
  85:              }
  86:              
  87:              // Calculate position, based on speed and direction
  88:              // Thanks for Matt Wolf for re-explaining trig to me
  89:              var ChangeX:Number = Math.sin(DegToRad(Direction)) * Speed;
  90:              var ChangeY:Number = Math.cos(DegToRad(Direction)) * Speed;
  91:   
  92:              PositionX += ChangeX;
  93:              PositionY -= ChangeY;
  94:                                          
  95:              // trace( "Speed: " + Speed );
  96:              // trace( "Direction: " + Direction );            
  97:          }
  98:   
  99:          // Convert the 0-360 direction into the format Flash uses
 100:          public function DirectionFlash():int
 101:          {
 102:              if ( Direction <= 180 )
 103:                  return Direction;
 104:              else
 105:                  return Direction - 360;
 106:          }
 107:          
 108:          public function DegToRad( Degree:Number ):Number
 109:          {
 110:              return Degree * Math.PI / 180;
 111:          }
 112:      }
 113:   
 114:  }