1

I am making a game using And Engine, when i tried to make it in generic way that works on all devices then i am in trouble...

I need to set a sprite on specific position of background sprite that should work on all screens... My background screen has same size as dimension of Device..

I tried to use pixel let say Glaxy S3 ha dimensions 720*1280

And i set according to it my sprite at location (584,608)

and my HTC experia ha dimensions (320,480)

SO i need to set it on (244,172) ....

I have tried all the following ways to set the position of sprites in generic way but not if any work...

I have tried alot of following things to make some formula that enables me to do it but unable to find any.. please advise

final Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

 metrics.density;
 metrics.densityDpi;
 metrics.heightPixels);
 metrics.scaledDensity);
 metrics.widthPixels);
 metrics.xdpi);
 metrics.ydpi);



Point point = getDisplaySize(display);
 CAMERA_WIDTH = point.x;
 CAMERA_HEIGHT = point.y;
 DisplayMetrics dm = new DisplayMetrics();     getWindowManager().getDefaultDisplay().getMetrics(dm);
 double x = Math.pow(dm.widthPixels/dm.xdpi,2); 
 double y = Math.pow(dm.heightPixels/dm.ydpi,2);
 double screenInches = Math.sqrt(x+y);
 int widthPix = (int) Math.ceil(dm.widthPixels * (dm.densityDpi / 160.0));

//CAMERA_WIDTH = display.getWidth();
//CAMERA_HEIGHT = display.getHeight();

Problem is little bit complex.. As i told above I tried to use pixel let say Glaxy S3 ha *dimensions 720*1280 And required sprite location is (584,608) so i set in manner ( CAMERA_WIDTH/1.233f,CAMERA_HEIGHT/2.112f)*

BUT HTC experia has dimensions *320*480 So the required positions according to ( CAMERA_WIDTH/1.233f,CAMERA_HEIGHT/2.112f) is (2599.5,227.27)* but this wrong according to display... when i set it on (244,172) for experia its working perfect.... please help.

Imran
  • 5,376
  • 2
  • 26
  • 45

1 Answers1

3

AndEngine offers multiple resolution policies. They are used to determine the size of the RenderSurfaceView, thus the actual size of the game on the screen, depending on the screen size.

You should use RatioResolutionPolicy: It will use the largest possible size, while keeping a constant ratio between the width and the height. By keeping the camera's size constant, everything it shows will be scaled by this constant ratio which will vary on different devices.

Example: Let CAMERA_WIDTH = 720 and CAMERA_HEIGHT = 480. If we create the camera and the engine options this way:

this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions options = new EngineOptions(..., new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);

The ratio is 720 / 480 = 1.5. When placing a sprite on your scene, use constant coordinates. Let's say we place it in the middle of the screen:

Sprite sprite = new Sprite(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, ...);

So:

  • On a device with screen size 720x480, the sprite will be placed at (360, 240) screen coordinates.
  • On a device with screen size 480x320, the sprite will be placed at (240, 160) screen coordinates.
  • On a device with screen size 800x480, the sprite will be placed at (400, 240) screen coordinates.

And so on...

Jong
  • 9,045
  • 3
  • 34
  • 66
  • Jong Thanks for comment but my problem is little bit complex.. As i told I tried to use pixel let say Glaxy S3 ha dimensions 720*1280 And required sprite location is (584,608) so i set in manner ( CAMERA_WIDTH/1.233f,CAMERA_HEIGHT/2.112f) BUT HTC experia has dimensions 320*480 So the required positions according to ( CAMERA_WIDTH/1.233f,CAMERA_HEIGHT/2.112f) is (2599.5,227.27) but this wrong according to display... when i set it on (244,172) for experia its working perfect.... please help.. – Imran Dec 09 '12 at 16:18
  • Hi , I had doubt I am using ratio resolution policy,in my base game activity i overridden the onsetContent View But after that margin is appearing only on one side How CAn i solve the issue ? Here is the link to my stack question http://stackoverflow.com/questions/14427152/how-to-override-onsetcontentview-while-using-ratio-resolution-policy-in-andengin – Renjith K N Jan 20 '13 at 18:33