The Train Gif JavaScript Tutorial

Train Movement Options

There's no need for trains to move at a constant speed. The createTrain function can make a train accelerate, decelerate, stop, and reverse. The following scenes demonstrate the various movement options available.

Acceleration
createTrain("Commute", "MBTAdemo", Right, 30, 2, 80, 3.5);
(Translation: train moves from left to right, starting at 30 MPH, and accelerates at 3.5 MPH/sec until it reaches 80 MPH.)

Deceleration
createTrain("Commute", "MBTAdemo", Right, 80, 2, 30, 3.5);
(Translation: train moves from left to right, starting at 80 MPH, and decelerates at 3.5 MPH/sec until it reaches 30 MPH.)

Stop and Proceed
createTrain("Commute", "MBTAdemo", Right, 40, 2, 0, 2.5);
(Translation: train moves from left to right, starting at 40 MPH, and decelerates to a stop at 2.5 MPH/sec, then accelerates at 2.5 MPH/sec until it reaches 40 MPH.)

Stop and Reverse
createTrack("east", 5, 1);
createTrain("Commute1", "MBTAdemo1", Right, 40, 2);
createTrack("west", 5, 2);
createTrain("Commute2", "MBTAdemo2", Right, 24, 2, 0, 1.5);
(Translation: first train moves at a constant 40 MPH from left to right, second train moves from left to right, starting at 24 MPH, and decelerates to a stop at 1.5 MPH/sec, reverses, then accelerates at 1.5 MPH/sec until it reaches 24 MPH.)

This scene also demonstrates multiple tracks.

Other Options

  1. You can have more than one train per track
  2. Trains on a track can run in opposite directions (sophisticated signaling prevents "cornfield meets":)