0

I'm trying to make a game for Android, I have a problem when dealing with multiple resolutions, this is a example of my gameview: http://oi42.tinypic.com/vh6ir6.jpg.

In the small area, there are several pieces of information, while the largest area is my play area. My purpose is to prevent the image (bitmap) from going beyond the edges of the playing area. This is partially working, but it only works with 800x432 resolution (the resolution of my tablet xD )

This is my code:

case MotionEvent.ACTION_MOVE:

    //left edge

                        if (deltaX <= 90 && deltaY <= 400 && deltaY >28){
                        Static_PositionX = 52;
                            Static_PositionY = (int)deltaY-35;
                            Drag = false;
                        }
//down edge
                        else if (deltaY >= 380 && deltaX > 90 && deltaX <770) { 
                          Static_PositionX =  (int)deltaX-42;
                          Static_PositionY = 360;
                          Drag = false;
//right edge
                         }else  if((deltaY <= 390 && deltaY > 20) && deltaX > 770){
                             Static_PositionX =723;
                            Static_PositionY = (int)deltaY-35;
                          Drag = false;

                         }
// up edge
                         else if ((deltaX >= 90 && deltaX < 770) && deltaY <= 35 ){
                            Static_PositionX =(int)deltaX-42;
                                Static_PositionY = 0;
                             Drag = false;

                    }

where deltaX and deltaY are my image coordinates and Static_PositionX/Static_PositionY are a random start position

deltaX=event.getX();
deltaY=event.getY();
yms
  • 10,361
  • 3
  • 38
  • 68

2 Answers2

1

Stop using absolute numbers for screen dimensions.

Refer to this answer here in SO on how to get them for current device.

Community
  • 1
  • 1
David Jashi
  • 4,490
  • 1
  • 21
  • 26
  • thx for your reply^^ but this how can help me?I have try with DisplayMetrics metrics but the values are 800x432(obviously)how i check when my image's deltaX/deltaY for edges limit? – Keyren Kuraun Jun 27 '13 at 20:40
  • Of course they are - you are checking them on YOUR device. What I'm saying is - check them once on start-up, put in some variables and use in your equations instead of actual numbers you have there. – David Jashi Jun 27 '13 at 20:52
  • And stop using "^" signs and smily faces - you are not on forum – David Jashi Jun 27 '13 at 20:53
  • ops sorry,anyway i'have try but if i do for example if(deltaX >= displaywidth ecc.) don't work because displaywidth = 800(in my case) and not 90(in my case position of left edge) – Keyren Kuraun Jun 27 '13 at 21:20
  • You are not supposed to replace **positions** with **dimensions**, aren't you? How did you calculate constant `400`? It's your horizontal screen dimension, divided by 2, isn't it? Well, do the same, but with variables. – David Jashi Jun 27 '13 at 21:23
  • actually control the position of my image (deltaX, deltaY) in the case MotionEvent.ACTION_UP: and based on those results I made the code, i made the control on variabiles deltaX and deltaY hat update constantly(ase MotionEvent.ACTION_MOVE:) – Keyren Kuraun Jun 27 '13 at 21:28
  • I can't help you here, if you are not familiar with a concept of usage of variables instead on constants. – David Jashi Jun 27 '13 at 21:32
  • what I can not figure out is how to replace the constants with variables width,height that change only based on display screen and not dynamically for check edge position, i remember you that my edge are custom draw with canvas.drawLine..check my example http://oi42.tinypic.com/vh6ir6.jpg – Keyren Kuraun Jun 27 '13 at 21:43
  • So what? You know size of your edge graphics? Can you subtract those dimensions from current screen dimensions? I guess you should be, it's simple arithmetic. What I think we have here is basic language barrier, because I frankly don't understand half of your sentences. You may not formulate your question in the right way and we may talk about totally different things. – David Jashi Jun 28 '13 at 07:17
  • @yms Then go ahead and edit his question and comments, if you understand them. I personally feel like banging my head against the wall. – David Jashi Jun 28 '13 at 14:26
  • @yms That's your opinion (and, as you can see, **only** yours) and you have all rights to keep it. English is in no way my native language, but when I address English-speaking audience (especially, asking for help) I'm trying to write without severe grammatical mistakes and take time to proof-read my epistles. Failure to do the same when addressing me personally is disrespect and I have right to request clarification in more understandable way. One can always use online spellcheckers or help of English-speaking friend, if he's not fluent enough. – David Jashi Jun 28 '13 at 14:49
  • sorry for my bad english,i'm italian and find it hard to explain this kind of thing is not in my language,anyway i'm try to be more clear,i have create my border in this way : `canvas.drawLine(55, 0, 55, Display_height, paint);//sx canvas.drawLine(0, 0, Display_width, 0, paint);//up canvas.drawLine(Display_width, 0, Display_width, Display_height, paint);//dx canvas.drawLine(0, Display_height, Display_width, Display_height, paint);//down` You can check the result in the example that I put at the beginning of this Thread – Keyren Kuraun Jun 28 '13 at 19:06
0

Design your game for a specific resolution.

boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
int frameBufferWidth = isLandscape ? 480 : 320;
int frameBufferHeight = isLandscape ? 320 : 480;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
            frameBufferHeight, Config.RGB_565);

Canvas canvas = new Canvas(frameBuffer); // Do all your drawing on frame buffer

When you actually render graphics on screen, draw the frame buffer on the actual canvas. This way all scaling for different screens would be handled for you.

Canvas canvas = holder.lockCanvas();
canvas.getClipBounds(dstRect);
canvas.drawBitmap(framebuffer, null, dstRect, null);                           
holder.unlockCanvasAndPost(canvas);
neo
  • 1,952
  • 2
  • 19
  • 39