简体   繁体   English

如何在Andengine场景中居中

[英]How to Center background in Andengine Scene

Is there a formula to use to center a background in the camera of Andengine. 是否有一个公式用于在Andengine的相机中居中背景。

My camera is the size of the Screen on the particular device which i retrieve using this... 我的相机是特定设备上屏幕的大小,我使用此检索...

final Display display = getWindowManager().getDefaultDisplay(); 
CAMERA_WIDTH = display.getWidth(); 
CAMERA_HEIGHT = display.getHeight();

int x = //formula to center background in camera or screen on the X axis

int y = //formula to center background in camera or screen on the Y axis

For my SpriteBackground, how could i figure out what is the x, and y to center it in the camera? 对于我的SpriteBackground,我怎么能弄清楚x是什么,而y在相机中居中?

The x and y is supplied like this.. x和y像这样提供..

Sprite sprite = new Sprite(x,y,TextureRegion);
final float x = CAMERA_WIDTH / 2 - textureRegion.getWidth() / 2;
final float y = CAMERA_HEIGHT / 2 - textureRegion.getHeight() / 2;
Sprite sprite = new Sprite(x, y, textureRegion);

This will make sprite positioned in the middle of the scene. 这将使精灵定位在场景的中间。 If the camera doesn't move around, it will be the center of the camera (hence screen) too. 如果相机没有移动,它也将成为相机的中心(因此也是屏幕)。

You should check setBackground method for your scene. 您应该检查场景的setBackground方法。 It will center your sprite and move it when your camera moves. 它将使您的精灵居中并在相机移动时移动它。

    Sprite background = new Sprite(0,0, CAMERA_WIDTH, CAMERA_HEIGHT, backgroundTextureRegion);
    scene.setBackground(new SpriteBackground(background));

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

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