简体   繁体   English

Android:Andengine设置精灵位置,适用于所有设备

[英]Android : Andengine setting sprite position that works on all devices

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... 我正在使用And Engine制作游戏,当我试图以通用方式制作它适用于所有设备然后我遇到麻烦...

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 我尝试使用像素让我们说Glaxy S3 ha尺寸为720 * 1280

And i set according to it my sprite at location (584,608) 我根据它设置了我的精灵(584,608)

and my HTC experia ha dimensions (320,480) 和我的HTC实验尺寸(320,480)

SO i need to set it on (244,172) .... 所以我需要设置它(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)* 问题有点复杂..正如我上面说的,我尝试使用像素让我们说Glaxy S3 ha *尺寸720 * 1280并且所需的精灵位置是(584,608)所以我设置方式(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. 但是HTC实验的尺寸为* 320 * 480所以根据(CAMERA_WIDTH / 1.233f,CAMERA_HEIGHT / 2.112f)所需的位置是(2599.5,227.27)*但这个错误根据显示...当我设置它(244,172)为了体验它的完美工作。 ... please help. ... 请帮忙。

AndEngine offers multiple resolution policies. AndEngine提供多种解决方案政策。 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. 它们用于确定RenderSurfaceView的大小,从而确定屏幕上游戏的实际大小,具体取决于屏幕大小。

You should use RatioResolutionPolicy : It will use the largest possible size, while keeping a constant ratio between the width and the height. 您应该使用RatioResolutionPolicy :它将使用尽可能大的尺寸,同时保持宽度和高度之间的恒定比率。 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 . 示例:设CAMERA_WIDTH = 720CAMERA_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 . 比率为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. 在屏幕尺寸为720x480的设备上,精灵将放置在(360,240)屏幕坐标处。
  • On a device with screen size 480x320, the sprite will be placed at (240, 160) screen coordinates. 在屏幕尺寸为480x320的设备上,精灵将放置在(240,160)屏幕坐标处。
  • On a device with screen size 800x480, the sprite will be placed at (400, 240) screen coordinates. 在屏幕尺寸为800x480的设备上,精灵将放置在(400,240)屏幕坐标处。

And so on... 等等...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM