如何使用AndEngine在不同的屏幕分辨率下运行我的游戏

5

我正在使用andeninge开发游戏。我固定了相机的宽度和高度。

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

@Override
public Engine onLoadEngine(){    
    this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    final Engine engine = new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new  FillResolutionPolicy(), this.mCamera).setNeedsSound(true));     
    return engine;
}

在游戏中,建筑物的图片尺寸为(1020x400)。当相机宽度和相机高度为480, 320时,建筑物的视图是正确的。如何使用andengine在不同的屏幕分辨率下运行我的游戏(使用相同的建筑图片尺寸)。

否则,我需要为所有不同的屏幕分辨率更改建筑物图片吗?

4个回答

5

如果您想要的话,仍可以像现在一样使用固定摄像头。OpenGL ES会将视图缩放以填充设备屏幕。这可能是最简单的解决方案,但在运行游戏的设备具有不同于1.5(480/320)的宽高比时,它将更改纵横比或在屏幕下/上或左/右留下黑色框。我认为目前大多数设备都有1.66的宽高比(800/480)。

另一个选择是使用:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics)
CAMERA_WIDTH = metrics.widthPixels()
CAMERA_HEIGHT = metrics.heightPixels()

要设置相机大小,然后使用像素大小和屏幕像素密度(dpi)的组合(请参见http://developer.android.com/reference/android/util/DisplayMetrics.html),然后使用.setScale将您的Sprites按比例缩放。


4
Andengine游戏可以原生地适应设备的缩放 - 因此,如果您将CAMERA_WIDTH/HEIGHT设置为480x320,并有人在分辨率为800x480的手机上运行它,则您的游戏将被放大到720x480(1.5倍),并且会有一个80px的边距(顶部,底部,左侧,右侧或根据您的视图重力分割)。您必须决定使用您的应用程序的大多数人将使用什么 - 我倾向于针对800x480并接受一些缩小到较小屏幕的情况,而不是反过来...

1
默认情况下,AndEngine 假定您需要固定的分辨率策略。但是,您也可以按照以下链接中所述进行更改。

http://android.kul.is/2013/10/andengine-tutorial-dealing-with-screen-sizes.html

或者,您可以遵循这个代码并相应地修改您的代码。(我的首选)

// Calculate the aspect ratio ofthe device.
float aspectRatio = (float) displayMetrics.widthPixels / (float) displayMetrics.heightPixels;

// Multiply the aspect ratio by the fixed height.
float cameraWidth = Math.round(aspectRatio * CAMERA_HEIGHT);

// Create the camera using those values.
this.mCamera = new Camera(0, 0, cameraWidth, cameraHeight);

// Pick some value for your height.
float cameraHeight = 320;

// Get the display metrics object.
final DisplayMetrics displayMetrics = new DisplayMetrics();

// Populate it with data about your display.
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

1
你可以使用这个源代码:
@Override
public Engine onLoadEngine() {
     final Display defaultDisplay = getWindow().getWindowManager().getDefaultDisplay();
     CAMERA_WIDTH = defaultDisplay.getWidth();
     CAMERA_HEIGHT = defaultDisplay.getHeight();
     this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
     return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接