Code in init frame

// monster traffic
// user controls a monster trying to fry cars
// By Andy Harris
// May 2005

init();
stop();

function init(){
  monster.dy = -5;
  car.dy = -5;
} //

//animate monster and car
monster.onEnterFrame = function(){
  monster._y += monster.dy;
  car._y += car.dy;

  //very limited boundary checking
  if (monster._y < 0){
    monster._y = Stage.height;
  } // end if

  if (car._y < 0){
    car._y = Stage.height;
  } // end if
} // end 

//button events
btnBegin.onRelease = function(){
  _root.gotoAndStop("play");
} // end button event

btnInstructions.onRelease = function(){
  _root.gotoAndStop("instructions");
} // end button event

Code in play frame


init();

function init(){
  //initialization

  //direction constants
  _root.NORTH = 0;
  _root.NORTHWEST = 1;
  _root.WEST = 2;
  _root.SOUTHWEST = 3;
  _root.SOUTH = 4;
  _root.SOUTHWEST = 5;
  _root.WEST = 6;
  _root.NORTHWEST = 7;

  //randomly position cars

  reset(car1);
  reset(car2);
  reset(car3);

  //set monster initial values
  monster.dx = 0;
  monster.dy = 0;
  monster.speed = 0;
  monster.dir = 0;
  monster.points = 0;
  monster.lives = 5;

  //set flame initial values
  flame.dx = 0;
  flame.dy = 0;
  flame.speed = 0;
  flame.dir = 0;

  //set up sounds
  sndCrash = new Sound();
	sndCrash.attachSound("crash.wav");

  sndFlame = new Sound();
	sndFlame.attachSound("flame.wav");

  sndAlarm = new Sound();
  sndAlarm.attachSound("alarm.wav");

} // end init

monster.onEnterFrame = function(){
  checkKeys();
  turn(monster);
  move(monster);

  //move the cars, too
  move(car1);
  move(car2);
  move(car3);

  //and the flame, if it's visible
  moveFlame();
  
  //check for collisions
  checkCollisions();

} // end monster enterFrame

function checkKeys(){
  //check keyboard to move monster
  if (Key.isDown(Key.UP)){
    monster.speed++;
    if (monster.speed > 8){
      monster.speed = 8;
    } // end if
  } // end if

  if (Key.isDown(Key.DOWN)){
    monster.speed--;
    if (monster.speed < -3){
      monster.speed = -3;
    } // end if
  } // end if

  if (Key.isDown(Key.RIGHT)){
    monster.dir++;
    if (monster.dir > 7){
      monster.dir = 0;
    } // end if
  } // end if

  if (Key.isDown(Key.LEFT)){
    monster.dir--;
    if (monster.dir < 0){
      monster.dir = 7;
    } // end if
  } // end if

  if (Key.isDown(Key.SPACE)){
    //shoot flame
    flame._x = monster._x;
    flame._y = monster._y;
    flame.dir = monster.dir;
    flame.speed = monster.speed + 20;
    turn(flame);
    sndFlame.start();
  } // end if

} // end checkKeys

function reset(sprite){
  //given a car sprite, resets its parameters
  sprite._x = Math.random() * Stage.width;
  sprite._y = Math.random() * Stage.height;
  sprite.dir = Math.random() * 8;
  sprite.dir = Math.floor(sprite.dir);
  sprite._rotation = sprite.dir * 45;
  sprite.speed = Math.random() * 10;
  turn(sprite);
} // end reset

function move(sprite){
  //moves any sprite, wrapping around boundaries

  //move
  sprite._x += sprite.dx;
  sprite._y += sprite.dy;

  //rotate
  sprite._rotation = sprite.dir * 45;
  
  //check boundaries - wrap all directions
  if (sprite._x > Stage.width){
    sprite._x = 0;
  } // end if

  if (sprite._x < 0){
    sprite._x = Stage.width;
  } // end if

  if (sprite._y > Stage.height){
    sprite._y = 0;
  } // end if

  if (sprite._y < 0){
    sprite._y = Stage.height;
  } // end if
} // end move

function moveFlame(){
  //the flame has different behavior on borders, so give it its own
  // movement function
  //move
  flame._x += flame.dx;
  flame._y += flame.dy;

  //rotate not needed
  //flame._rotation = flame.dir * 45;
  
  //hide on all boundaries
  if ((flame._x > Stage.width) ||
      (flame._x < 0) ||
      (flame._y > Stage.height) ||
      (flame._y < 0)){
     resetFlame();
  } // end if

} // end moveFlame

function resetFlame(){
  //move off screen and stop
  flame._x = - 200;
  flame._y = - 200; 
  flame.speed = 0;
} // end resetFlame

function turn(sprite){
  //given a sprite with a dir and speed, calculates dx and dy values

  switch (sprite.dir){
    case 0:
      sprite.dx = 0 * sprite.speed;
      sprite.dy = -1 * sprite.speed;
      break;
    case 1:
      sprite.dx = .7 * sprite.speed;
      sprite.dy = -.7 * sprite.speed;
      break;
    case 2:
      sprite.dx = 1 * sprite.speed;
      sprite.dy = 0 * sprite.speed;
      break;
    case 3:
      sprite.dx = .7 * sprite.speed;
      sprite.dy = .7 * sprite.speed;
      break;
    case 4:
      sprite.dx = 0 * sprite.speed;
      sprite.dy = 1 * sprite.speed;
      break;
    case 5:
      sprite.dx = -.7 * sprite.speed;
      sprite.dy = .7 * sprite.speed;
      break;
    case 6:
      sprite.dx = -1 * sprite.speed;
      sprite.dy = 0 * sprite.speed;
      break;
    case 7:
      sprite.dx = -.7 * sprite.speed;
      sprite.dy = -.7 * sprite.speed;
      break;
    default:
      trace ("Something went wrong here...");
     break;
  } // end switch
} // end turn

function checkCollisions(){
  //check for various collisions

  if (monster.hitTest(car1)){
    trace("hit by car 1!");
    sndCrash.start();
    reset(car1);
    monster.lives--;
    if (monster.lives <= 0){
      _root.gotoAndStop("gameOver");
    } // end if
  } // end if

  if (monster.hitTest(car2)){
    trace("hit by car 2!");
    sndCrash.start();
    reset(car2);
    monster.lives--;
    if (monster.lives <= 0){
      _root.gotoAndStop("gameOver");
    } // end if
  } // end if

  if (monster.hitTest(car3)){
    trace("hit by car 3!");
    sndCrash.start();
    reset(car3);
    monster.lives--;
    if (monster.lives <= 0){
      _root.gotoAndStop("gameOver");
    } // end if
  } // end if

  //look for flame collisions
  if (flame.hitTest(car1)){
    trace("burned car 1");
    sndAlarm.start();
    resetFlame();
    reset(car1);
    monster.points += 100;
  } // end if

  if (flame.hitTest(car2)){
    trace("burned car 2");
    sndAlarm.start();
    resetFlame();
    reset(car2);
    monster.points += 100;
  } // end if

  if (flame.hitTest(car3)){
    trace("burned car 3");
    sndAlarm.start();
    resetFlame();
    reset(car3);
    monster.points += 100;
  } // end if
} // end checkCollisions

Code in gameOver frame


init();
stop();

function init(){
  car._x = 380;
  car._y = 40;
  car.dy = -5;
  
  monster._x = 380;
  monster._y = 250;
  monster._rotation = 0;
  monster.dy = -5;  
} //

//animate monster and car
monster.onEnterFrame = function(){
  monster._y += monster.dy;
  car._y += car.dy;

  //very limited boundary checking
  if (monster._y < 0){
    monster._y = Stage.height;
  } // end if

  if (car._y < 0){
    car._y = Stage.height;
  } // end if
} // end 

//button event
btnBegin.onRelease = function(){
  _root.gotoAndStop("play");
} // end button event

Code in instructions frame


init();
stop();

function init(){
  car._x = 485;
  car._y = 40;
  car.dy = -5;
  
  monster._x = 485;
  monster._y = 250;
  monster._rotation = 0;
  monster.dy = -5;  
} //

//animate monster and car
monster.onEnterFrame = function(){
  monster._y += monster.dy;
  car._y += car.dy;

  //very limited boundary checking
  if (monster._y < 0){
    monster._y = Stage.height;
  } // end if

  if (car._y < 0){
    car._y = Stage.height;
  } // end if
} // end 

//button event
btnBegin.onRelease = function(){
  _root.gotoAndStop("play");
} // end button event