Book Image

Learning Java by Building Android Games

By : John Horton
Book Image

Learning Java by Building Android Games

By: John Horton

Overview of this book

Table of Contents (17 chapters)
Learning Java by Building Android Games
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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...