Verm1ll1on

Pong in ActionScript3

May 21st, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.display.MovieClip;
  2. import flash.events.Event;
  3. import fl.motion.MotionEvent;
  4. import flash.events.KeyboardEvent;
  5.  
  6. var batspeed:Number = 10;
  7. //Regra 4
  8. var velocidade:Number=4;
  9. var up: Boolean = false;
  10. var down: Boolean = false;
  11. var up2: Boolean = false;
  12. var down2: Boolean = false;
  13. //Regra 1
  14. var Bola:MovieClip= new Ball();
  15. stage.addChild(Bola);
  16. var BolaX:Number=1;
  17. var BolaY:Number=1;
  18. //Regra 2
  19. Bola.x = 275;
  20. Bola.y = 200;
  21. //Regra 1
  22. var Start:MovieClip = new Begins();
  23. stage.addChild(Start);
  24. Start.x = 275;
  25. Start.y = 200;
  26. //Bola pelo stage
  27. stage.addEventListener(Event.ENTER_FRAME, OnEnFr);
  28. function OnEnFr (e:Event):void
  29. {
  30.     //Regra 4 e Regra 5
  31.     Bola.x += BolaX * velocidade;
  32.     Bola.y += BolaY * velocidade;
  33.     if (Bola.y >= stage.stageHeight - Bola.height)
  34.     {
  35.         BolaY = -BolaY;
  36.     }
  37.      if(Bola.y <= 0)
  38.     {
  39.         BolaY = -BolaY;
  40.     }
  41.     if (Bola.x >= stage.stageWidth - Bola.width)
  42.     {
  43.         BolaX = -BolaX;
  44.     }
  45.      if(Bola.x <= 0)
  46.     {
  47.         BolaX = -BolaX;
  48.     }
  49. }
  50. //Regra 1
  51. var Batedor1:MovieClip= new Player1();
  52. var Batedor2:MovieClip= new Player2();
  53. stage.addEventListener(MouseEvent.CLICK, OnCl);
  54. //Funcao que vai adicionar batedores, placares zerados
  55. function OnCl (e:MouseEvent) : void
  56. {
  57.     stage.removeEventListener(Event.ENTER_FRAME, OnEnFr);
  58.     stage.removeChild(Start);
  59.     stage.removeChild(Bola);
  60.     stage.addChild(Batedor1);
  61.     //Regra 7
  62.     Batedor1.x = 4;
  63.     Batedor1.y = 200;
  64.     stage.addChild(Batedor2);
  65.     //Regra 8
  66.     Batedor2.x = 546;
  67.     Batedor2.y = 200;
  68.     stage.addChild(Bola);
  69.     //Regra 2
  70.     Bola.x = 275;
  71.     Bola.y = 200;
  72.     BolaX = 1;
  73.     BolaY = 1;
  74. }
  75. //Código para movimento de bola, colisão e limites
  76. stage.addEventListener(Event.ENTER_FRAME, BallIG);
  77. stage.addEventListener(Event.ENTER_FRAME, placares);
  78. function BallIG (e:Event) : void
  79. {
  80.     //Velocidade Inicial da Bola
  81.     Bola.x += BolaX * 4;
  82.     Bola.y += BolaY * 4;
  83.     //Limite da Bola no Stage
  84.     if (Bola.y > stage.stageHeight || Bola.y < 0)
  85.     {
  86.         //Regra 9 e 10
  87.         BolaY = -BolaY;
  88.     }
  89.     //Colisão no Batedor 1 e 2
  90.     //Regra 11 e 12
  91.     if (Bola.hitTestObject(Batedor1) || Bola.hitTestObject(Batedor2))
  92.     {
  93.         BolaX = -BolaX;
  94.     }
  95. }
  96. //Mudar  o Placar e fazer Bola Retornar ao centro do stage
  97. function placares (e:Event) : void
  98. {
  99.     //Regra 6
  100.     if (Bola.x < 0 - Bola.width)
  101.     {
  102.         //Regra 18
  103.         ScorePl2.text = (int(ScorePl2.text) +1).toString();
  104.         Bola.x = 275;
  105.         Bola.y = 200;
  106.         //Regra 13
  107.         if(BolaY < 0)
  108.             {
  109.                 BolaY = (BolaY * -1);          
  110.             }
  111.             BolaX = BolaX - 1;
  112.             BolaY = BolaY + 1;
  113.     }
  114.     if (Bola.x > stage.stageWidth + Bola.width)
  115.     {
  116.         //Regra 18
  117.         ScorePl1.text = (int(ScorePl1.text)+1).toString();
  118.         Bola.x = 275;
  119.         Bola.y = 200;
  120.         //Regra 13
  121.         if(BolaY < 0)
  122.             {
  123.                 BolaY = BolaY * -1;        
  124.             }
  125.         BolaX = BolaX + 1;
  126.         BolaY = BolaY + 1;
  127.     }
  128.     //Regra 19
  129.     if (ScorePl1.text == "10")
  130.     {
  131.         stage.removeEventListener(Event.ENTER_FRAME, BallIG);
  132.         var Victory1:MovieClip= new Victory1MC();
  133.         stage.addChild(Victory1);
  134.         Victory1.x = 275;
  135.         Victory1.y = 200;
  136.     }
  137.     //Regra 19
  138.     if (ScorePl2.text == "10")
  139.     {
  140.         stage.removeEventListener(Event.ENTER_FRAME, BallIG);
  141.         var Victory2:MovieClip= new Victory2MC();
  142.         stage.addChild(Victory2);
  143.         Victory2.x = 275;
  144.         Victory2.y = 200;
  145.     }
  146.    
  147. }
  148. //Movimento do Batedor 1
  149. stage.addEventListener(KeyboardEvent.KEY_UP, OnKeUp);
  150. stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeDo);
  151. stage.addEventListener(KeyboardEvent.KEY_UP, OnKeUp2);
  152. stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeDo2);
  153. function OnKeDo (e:KeyboardEvent) : void
  154. {
  155.     //Regra 14
  156.     if (e.keyCode == 87)
  157.     {
  158.         up = true;
  159.     }
  160.     if (e.keyCode == 83)
  161.     {
  162.         down = true;
  163.     }
  164.     if(Batedor1.y < 0)
  165.     {
  166.         Batedor1.y = 0
  167.     }
  168.     if (Batedor1.y > stage.stageHeight - Batedor1.height)
  169.     {
  170.         Batedor1.y = stage.stageHeight - Batedor1.height;
  171.     }
  172. }
  173. function OnKeUp (e:KeyboardEvent) : void
  174. {
  175.     if (e.keyCode == 87)
  176.     {
  177.         up = false;
  178.     }
  179.     if (e.keyCode == 83)
  180.     {
  181.         down = false;
  182.     }
  183. }
  184. stage.addEventListener(Event.ENTER_FRAME, OnEnFrPl1);
  185. stage.addEventListener(Event.ENTER_FRAME, OnEnFrPl2);
  186. function OnEnFrPl1 (e:Event) : void
  187. {
  188.     if (up && (Batedor1.y - (Batedor1.height / 2)) > 0)
  189.     {
  190.         Batedor1.y -= batspeed;
  191.     }
  192.     if (down && (Batedor1.y + (Batedor1.height / 2)) < stage.stageHeight )
  193.     {
  194.         Batedor1.y += batspeed;
  195.     }
  196.    
  197. }
  198. //Movimento do Batedor 2
  199. function OnKeDo2 (e:KeyboardEvent) : void
  200. {
  201.     //Regra 15
  202.     if (e.keyCode == 38)
  203.     {
  204.         up2 = true;
  205.     }
  206.     if (e.keyCode == 40)
  207.     {
  208.         down2 = true;
  209.     }
  210. }
  211. function OnKeUp2 (e:KeyboardEvent) : void
  212. {
  213.     if (e.keyCode == 38)
  214.     {
  215.         up2 = false;
  216.     }
  217.     if (e.keyCode == 40)
  218.     {
  219.         down2 = false;
  220.     }
  221. }
  222. function OnEnFrPl2 (e:Event) : void
  223. {
  224.     //Regra 16
  225.     if (up2 && (Batedor2.y - (Batedor2.height / 2)) > 0)
  226.     {
  227.         Batedor2.y -= batspeed;
  228.     }
  229.     if (down2 && (Batedor2.y + (Batedor2.height / 2)) < stage.stageHeight )
  230.     {
  231.         Batedor2.y += batspeed;
  232.     }
  233. }
  234. //Regra 17 está no Stage
Advertisement
Add Comment
Please, Sign In to add comment