Chapter 7
Q1) The speed of the ball is calculated in pixels. Different devices have different numbers of pixels. Can you explain how to make the ball speed approximately the same on different screen resolutions?
A) A simple way to accommodate different screen resolutions would be to devise a system that that takes into account the number of pixels the screen has. We have already done this for the racket and ball sizes. We could declare a member variable like this:
int pixelsPerFrameX; int pixelsPerFrameY;
We could then initialize these variables in onCreate
after we have obtained the screen dimensions:
pixelsPerFrameX = screenWidth/50; pixelsPerFrameY = screenHeight/50;
Then we can move our ball a bit, like this:
//moving in adjust our x any positions if (ballIsMovingDown) { ballPosition.y += pixelsPerFrameX; } //etc...