How to make my Android Game compatible with all screen sizes


#1

Hello guys, i have a big problem. I am almost ready to publish my first game in google play, but i cannot figure out how to make the app compatible with all the different sizes of screens (1280x720, 1920x1080 etc.) . Here is a simple examble of what i am trying to do:

fullScreen(P2D);

if(mousePressed && mouseX > width - 89/100 * width && mouseX < width - 70/100 * width && mouseY > height - 26/100 * height && mouseY < height - 12/100 * height){

                               background(0);

So, the “if” represents a red button that is going to start the game. I started the game with a 1280x720 scren, but i remembered that i want to make the app compatible with all devices, so:

The first “if” was this:

if(mousePressed && mouseX > 140 && mouseX < 390 && mouseY > 530 && mouseY < 630){
   
                                background(0);

So i found how many % of 1280 is 140 and replaced it with “if(…&& mouseX > width - 89/100 * width…” and i did the same thing (with different percentages of course) with all the mouseY and mouseX’s, but it didn’t work… I really don’t know what to do, please help me! Thanks!


#2

Can you please try to be more specific? What do you mean when you say it didn’t work?

Can you post a small example program that demonstrates the problem?


#3

void setup(){
fullScreen(P2D);
}

void draw(){
if(mousePressed && mouseX > width - 89/100 * width && mouseX < width - 70/100 * width && mouseY > height - 26/100 * height && mouseY < height - 12/100 * height){

background(0);

The idea is when i press that part of the screen, to set the background to 0.

There’s too much code to post it all here


#4

Sounds like you need to debug your code. Try splitting that line up into multiple steps and printing out the values of each step.

Specifically, I’m guessing that your division is not working how you expect.


#5

So, when i do that, it tells me my value for mouseX is 1920, but when i post the same calculation in google, it tells me that its 140. I don’t know why.


#6

uh, i used 0.89 instead of 89/100 and it worked. Strange. Thanks for the answer!


#7

Look into integer division. Basically, an int value can only hold whole numbers, without any decimal places. Since 70 and 100 are both int values, the result is an int as well. And since it can’t hold a decimal place, it ends up being 0 instead of 0.7.

Try it out with a simpler example:

int x = 70;
int y = 100;
int z = x / y;
println(z);

#8

I figured it out, thanks alot!