Advertisement
Guest User

Untitled

a guest
Mar 10th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //init(); //call this function once, at the very beginning, to initialize everything  
  2. //    
  3. // function init():void
  4. // {  
  5. //     stage.addEventListener(Event.ENTER_FRAME, loop); //every time our game enters a new frame (60 times per second), this event listener will fire and our game will loop  
  6. // }  
  7. //    
  8. // function loop(e:Event):void
  9. // {  
  10. //    
  11. // }
  12. // var ballSpeedX:int = -3;  
  13. // var ballSpeedY:int = -2;  
  14. //    
  15. // init();  
  16. //    
  17. // function init():void
  18. // {  
  19. //     stage.addEventListener(Event.ENTER_FRAME, loop);  
  20. // }  
  21. //    
  22. // function loop(e:Event):void
  23. // {  
  24. //     ball.x += ballSpeedX;  
  25. //     ball.y += ballSpeedY;  
  26. //  
  27. //     //because the ball's position is measured by where its CENTER is...  
  28. //     //...we need add or subtract half of its width or height to see if that SIDE is hitting a wall  
  29. //    
  30. //     //first check the left and right boundaries  
  31. //     if(ball.x <= ball.width/2){ //check if the x position of the left side of the ball is less than or equal to the left side of the screen, which would be 0  
  32. //         ball.x = ball.width/2; //then set the ball's x position to that point, in case it already moved off the screen  
  33. //         ballSpeedX *= -1; //and multiply the ball's x speed by -1, which will make it move right instead of left  
  34. //    
  35. //     } else if(ball.x >= stage.stageWidth-ball.width/2){ //check to see the right side of the ball is touching the right boundary, which would be 550  
  36. //         ball.x = stage.stageWidth-ball.width/2; //reposition it, just in case  
  37. //         ballSpeedX *= -1; //multiply the x speed by -1 (now moving left, not right)  
  38. //    \
  39. //     }  
  40. //    
  41. //     //now we do the same with the top and bottom of the screen  
  42. //     if(ball.y <= ball.height/2){ //if the y position of the top of the ball is less than or equal to the top of the screen  
  43. //         ball.y = ball.height/2; //like we did before, set it to that y position...  
  44. //         ballSpeedY *= -1; //...and reverse its y speed so that it is now going down instead of up  
  45. //    
  46. //     } else if(ball.y >= stage.stageHeight-ball.height/2){ //if the bottom of the ball is lower than the bottom of the screen  
  47. //         ball.y = stage.stageHeight-ball.height/2; //reposition it  
  48. //         ballSpeedY *= -1; //and reverse its y speed so that it is moving up now  
  49. //    
  50. //     }  
  51. // }
  52.  
  53. var ballSpeedX:int = -3;  
  54.  
  55.  var ballSpeedY:int = -2;  
  56.  
  57.    
  58.  
  59.  init();  
  60.  
  61.    
  62.  
  63.  function init():void
  64.  
  65.  {  
  66.  
  67.      stage.addEventListener(Event.ENTER_FRAME, loop);  
  68.  
  69.  }  
  70.  
  71.    
  72.  
  73.  function loop(e:Event):void
  74.  
  75.  {  
  76.  
  77.      ball.x += ballSpeedX;  
  78.  
  79.      ball.y += ballSpeedY;  
  80.  
  81.    
  82.  
  83.      //because the ball's position is measured by where its CENTER is...  
  84.  
  85.      //...we need add or subtract half of its width or height to see if that SIDE is hitting a wall  
  86.  
  87.    
  88.  
  89.      //first check the left and right boundaries  
  90.  
  91.      if(ball.x <= ball.width/2){ //check if the x position of the left side of the ball is less than or equal to the left side of the screen, which would be 0  
  92.  
  93.          ball.x = ball.width/2; //then set the ball's x position to that point, in case it already moved off the screen  
  94.  
  95.          ballSpeedX *= -1; //and multiply the ball's x speed by -1, which will make it move right instead of left  
  96.  
  97.    
  98.  
  99.      } else if(ball.x >= stage.stageWidth-ball.width/2){ //check to see the right side of the ball is touching the right boundary, which would be 550  
  100.  
  101.          ball.x = stage.stageWidth-ball.width/2; //reposition it, just in case  
  102.  
  103.          ballSpeedX *= -1; //multiply the x speed by -1 (now moving left, not right)  
  104.  
  105.    
  106.  
  107.      }  
  108.  
  109.    
  110.  
  111.      //now we do the same with the top and bottom of the screen  
  112.  
  113.      if(ball.y <= ball.height/2){ //if the y position of the top of the ball is less than or equal to the top of the screen  
  114.  
  115.          ball.y = ball.height/2; //like we did before, set it to that y position...  
  116.  
  117.         ballSpeedY *= -1; //...and reverse its y speed so that it is now going down instead of up  
  118.  
  119.    
  120.  
  121.      } else if(ball.y >= stage.stageHeight-ball.height/2){ //if the bottom of the ball is lower than the bottom of the screen  
  122.  
  123.          ball.y = stage.stageHeight-ball.height/2; //reposition it  
  124.  
  125.          ballSpeedY *= -1; //and reverse its y speed so that it is moving up now  
  126.  
  127.    
  128.  
  129.      }  
  130.  
  131.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement