Don't like ads? PRO users don't see any ads ;-)
Guest

GOGIOGOGOGO

By: a guest on May 17th, 2012  |  syntax: None  |  size: 1.77 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var xDirection:Number = 10;
  2. var yDirection:Number = -10;
  3.  
  4. var targetX:Number = paddle_mc.x;
  5. var easing:Number = 7;
  6.  
  7. function resetBallPosition():void
  8. {
  9.         xDirection = 10;
  10.         yDirection = -10;
  11.         ball_mc.x = paddle_mc.x + paddle_mc.width/2 - ball_mc.width/2;
  12.         ball_mc.y = paddle_mc.y - ball_mc.height - paddle_mc.height/2;
  13. }
  14.  
  15. function checkHitLocation(paddle:MovieClip):void
  16. {
  17.         var hitPercent:Number;
  18.         var ballPosition:Number = ball_mc.x - paddle.x;
  19.         hitPercent = (ballPosition / (paddle.width - ball_mc.width)) - .5;
  20.         xDirection = hitPercent * 30;
  21.         yDirection *=1.05;     
  22. }
  23.  
  24. function initializeGame(event:MouseEvent):void
  25. {
  26.         paddle_mc.addEventListener(Event.ENTER_FRAME, movePaddle);
  27.         ball_mc.addEventListener(Event.ENTER_FRAME, moveBall);
  28.         bg_mc.removeEventListener(MouseEvent.CLICK, initializeGame);
  29.        
  30.         Mouse.hide();
  31. }
  32.  
  33. function moveBall(event:Event):void
  34. {
  35.         if(ball_mc.x <= 0)
  36.         {
  37.                 xDirection *= -1;
  38.         }
  39.         else if(ball_mc.x >= stage.stageWidth - ball_mc.width)
  40.         {
  41.                 xDirection *= -1;
  42.         }
  43.         if(ball_mc.hitTestObject(paddle_mc))
  44.         {
  45.                 yDirection *= -1;
  46.                 ball_mc.y = paddle_mc.y - ball_mc.height - paddle_mc.height/2;
  47.                 checkHitLocation(paddle_mc);
  48.         }
  49.         if(ball_mc.y <= 0)
  50.         {
  51.                 yDirection *= -1;
  52.                 //resetBallPosition();
  53.         }
  54.         else if(ball_mc.y >= stage.stageHeight - ball_mc.height)
  55.         {
  56.                 resetBallPosition();
  57.         }
  58.         ball_mc.x += xDirection;
  59.         ball_mc.y += yDirection;
  60.        
  61. }
  62.  
  63. function movePaddle(event:Event):void
  64. {
  65.         if(this.mouseX <= paddle_mc.width/2)
  66.         {
  67.                 targetX = 0;
  68.         }
  69.         else if(this.mouseX >= stage.stageWidth - paddle_mc.width/2)
  70.         {
  71.                 targetX = stage.stageWidth - paddle_mc.width;
  72.         }
  73.         else
  74.         {
  75.         targetX = this.mouseX - paddle_mc.width/2;
  76.         }
  77.         paddle_mc.x += (targetX - paddle_mc.x) / easing;
  78. }
  79.  
  80. bg_mc.addEventListener(MouseEvent.CLICK, initializeGame);