Pringleman

Future Learn Week 2 Keep Ball in Canvas

Oct 15th, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.95 KB | None | 0 0
  1.  @Override protected void updateGame(float secondsElapsed) {
  2.         /*Detect if the ball is touching or has passed the edge of the screen.
  3.           If it has, reverse the speed of the ball and slow it down slightly (making it appear
  4.           to bounce back, losing 10% power)
  5.          */
  6.         if (mBallX >= mCanvasWidth) {
  7.             mBallX = mCanvasWidth;
  8.             mBallSpeedX = 0f - mBallSpeedX;
  9.         }
  10.  
  11.         if (mBallX <= 0) {
  12.             mBallX = 0;
  13.             mBallSpeedX = 0f - mBallSpeedX;
  14.         }
  15.  
  16.         if (mBallY >= mCanvasHeight) {
  17.             mBallY = mCanvasHeight;
  18.             mBallSpeedY = 0f - mBallSpeedY;
  19.         }
  20.  
  21.         if (mBallY <= 0) {
  22.             mBallY = 0;
  23.             mBallSpeedY = 0f - mBallSpeedY;
  24.         }
  25.  
  26.         //Move the ball's X and Y using the speed (pixel/sec)
  27.         mBallX = mBallX + secondsElapsed * mBallSpeedX;
  28.         mBallY = mBallY + secondsElapsed * mBallSpeedY;
  29.  
  30.         }
Add Comment
Please, Sign In to add comment