home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 June / Chip_2004-06_cd1.bin / oddech / table / TableFootie.swf / scripts / DefineSprite_241 / frame_1 / DoAction.as
Encoding:
Text File  |  2004-05-05  |  3.5 KB  |  198 lines

  1. function checkForWinner()
  2. {
  3.    if(homeScore == 5 || awayScore == 5)
  4.    {
  5.       _root.player1Score = homeScore;
  6.       _root.player2Score = awayScore;
  7.       _root.gotoAndPlay("gameOver");
  8.    }
  9. }
  10. function author()
  11. {
  12.    author = "All actionscript by Karl Berseus for NakedPenguinBoy.";
  13. }
  14. function runGame()
  15. {
  16.    playerMove();
  17.    pl2Move();
  18.    var scorer;
  19.    if((scorer = ball.move()) != 0)
  20.    {
  21.       trace(scorer + " scores");
  22.       goal(scorer);
  23.    }
  24.    if(!hitT(1))
  25.    {
  26.       hitT(-1);
  27.    }
  28. }
  29. function playerMove()
  30. {
  31.    if(Key.isDown(38) && pl1._y > -110)
  32.    {
  33.       pl1.move(- plS);
  34.       pl1.ySpeed = 0.7853981633974483;
  35.    }
  36.    else if(Key.isDown(40) && pl1._y < 110)
  37.    {
  38.       pl1.move(plS);
  39.       pl1.ySpeed = -0.7853981633974483;
  40.    }
  41.    else
  42.    {
  43.       pl1.move(0);
  44.       pl1.ySpeed = 0;
  45.    }
  46. }
  47. function secondPlayerMove()
  48. {
  49.    if(Key.isDown(65) && pl2._y > -110)
  50.    {
  51.       pl2.move(- plS);
  52.       pl2.ySpeed = 0.7853981633974483;
  53.    }
  54.    else if(Key.isDown(90) && pl2._y < 110)
  55.    {
  56.       pl2.move(plS);
  57.       pl2.ySpeed = -0.7853981633974483;
  58.    }
  59.    else
  60.    {
  61.       pl2.move(0);
  62.       pl2.ySpeed = 0;
  63.    }
  64. }
  65. function aiMove()
  66. {
  67.    var ball_y = ball.getGlobY();
  68.    var goalie_y = pl2.r1.m0.getGlobY();
  69.    if(ball._x > aiG_l._x || ball._x < aiD_l._x)
  70.    {
  71.       if(ball_y > goalie_y)
  72.       {
  73.          aiSpeed += 1;
  74.       }
  75.       else
  76.       {
  77.          aiSpeed -= 1;
  78.       }
  79.    }
  80.    else
  81.    {
  82.       var attacker_y;
  83.       if(ball_y < ball.og_globY)
  84.       {
  85.          attacker_y = pl2.r2.m0.getGlobY();
  86.       }
  87.       else
  88.       {
  89.          attacker_y = pl2.r2.m1.getGlobY();
  90.       }
  91.       if(ball_y > attacker_y)
  92.       {
  93.          aiSpeed += 1;
  94.       }
  95.       else
  96.       {
  97.          aiSpeed -= 1;
  98.       }
  99.    }
  100.    if(aiSpeed < - aiS)
  101.    {
  102.       aiSpeed = - aiS;
  103.       pl2.ySpeed = 0.7853981633974483;
  104.    }
  105.    else if(aiSpeed > aiS)
  106.    {
  107.       aiSpeed = aiS;
  108.       pl2.ySpeed = -0.7853981633974483;
  109.    }
  110.    if(aiSpeed > 4 || aiSpeed < -4)
  111.    {
  112.       pl2.move(aiSpeed / difConst);
  113.    }
  114.    else
  115.    {
  116.       pl2.move(0);
  117.       pl2.ySpeed = 0;
  118.    }
  119. }
  120. function hitT(pl)
  121. {
  122.    var margin = ball.r + 7;
  123.    var player;
  124.    if(pl == 1)
  125.    {
  126.       player = pl1;
  127.    }
  128.    else
  129.    {
  130.       player = pl2;
  131.    }
  132.    for(var row in player)
  133.    {
  134.       if(ball._x > player[row]._x - margin && ball._x < player[row]._x + margin)
  135.       {
  136.          var r = player[row];
  137.          var ball_y = ball.getGlobY();
  138.          for(var man in r)
  139.          {
  140.             var man_y = r[man].getGlobY();
  141.             if(ball_y > man_y - margin && ball_y < man_y + margin && r[man].gr._currentframe == 1)
  142.             {
  143.                r[man].hit();
  144.                ball.hit(pl);
  145.                return true;
  146.             }
  147.          }
  148.       }
  149.    }
  150.    return false;
  151. }
  152. function reset()
  153. {
  154.    ball.reset();
  155.    efActions.gotoAndStop(2);
  156. }
  157. function goal(scorer)
  158. {
  159.    if(scorer == 1)
  160.    {
  161.       homeScore += 1;
  162.       scoreBoard.homeP = homeScore;
  163.       checkForWinner();
  164.    }
  165.    else
  166.    {
  167.       awayScore += 1;
  168.       scoreBoard.awayP = awayScore;
  169.       checkForWinner();
  170.    }
  171.    ball._visible = false;
  172.    efActions.gotoAndStop(1);
  173.    goalScreen.play();
  174.    pl1.move(0);
  175.    pl2.move(0);
  176. }
  177. function ballClose()
  178. {
  179.    closeSound.play();
  180. }
  181. homeScore = 0;
  182. awayScore = 0;
  183. aiSpeed = 0;
  184. plS = 5;
  185. aiS = 7;
  186. difConst = _root.diffLevel;
  187. aiG = 0;
  188. trace(difConst);
  189. pl2Move = _root.noOfPlayers;
  190. if(_root.noOfPlayers == 2)
  191. {
  192.    pl2Move = secondPlayerMove;
  193. }
  194. else
  195. {
  196.    pl2Move = aiMove;
  197. }
  198.