Soccer Source code

//define constants for positions
_root.SC = 0;  //score
_root.CT = 1;  //center
_root.WI = 2;  //wing
_root.HB = 3;  //halfback
_root.FB = 4;  //fullback
_root.GK = 5;  //goalkeeper

//define array for player names
_root.plName = new Array( "score", "center", "wing", "halfback", "fullback", "goalkeeper");

_root.ballCarrier = _root.GK;

init();

function init(){
  //set up array

  //define arrays to hold rows
  var SCVals = new Array(.50, -1, .70, .30, .80, .80);
  var CTVals = new Array(.30, .60, -1, .70, .80, .80);
  var WIVals = new Array(.25, .50, .70, -1, .80, .80);
  var HBVals = new Array(.05, .40, .50, .70, -1, .80);
  var GKVals = new Array(.01, .05, .10, .30, .90, -1);

  //make an array of arrays
  _root.shotPerc = new Array (0, SCVals, CTVals, WIVals, HBVals, GKVals);

  //give each player an ID property and mouseUp function

  for (playerNum = 0; playerNum <= 5; playerNum++){
    thePlayer = eval("_root." + plName[playerNum]);
    thePlayer.id = playerNum;
    thePlayer.onMouseUp = function(){
    //respond only if mouse is over me (aaaargh!)
    if (this.hitTest(_root._xmouse, _root._ymouse, false)){
      //check for a successful pass
      shot = Math.random();
      if (shot < shotPerc[_root.ballCarrier][this.id]){
        //advance the ball
	_root.ballCarrier = this.id;
	 //check for a score
	 if (this.id == _root.SC){
           trace("goal!");
	   _root.ballCarrier = _root.HB;
	 } // end 'scored' if
	 showBall();
	 trace ("good pass");
      } else {
	trace ("no good");
        _root.ballCarrier++;
	 showBall();
	 if (_root.ballCarrier > 5){
           trace("opponent scores!");
	   _root.ballCarrier = _root.HB;
	   showBall();
	 } // end if
       } // end 'shot works' if
     } // end 'mouse is over me' if
    } // end mouseUp function def
    
  } // end for loop

  showBall();

} // end init

function showBall(){
  //ensure appropriate player clip has the ball
  for (playerNum = 1; playerNum <= 5; playerNum++){
    thePlayer = eval("_root." + plName[playerNum]);
    
    if (_root.ballCarrier == playerNum){
      thePlayer.state = "ball";
    } else {
      thePlayer.state = "noBall";
    } // end if
    thePlayer.gotoAndStop(thePlayer.state);

  } // end for
} // end showBall