AndEngine全图像背景(宽度和高度)

时间:2015-10-30 08:49:46

标签: android box2d andengine game-engine

我是Android游戏开发的新手,我正面临着有关屏幕图像背景的问题。

以下是我的完整课程代码SimpleBaseGameActivity 与图像尺寸对应的更新相机宽度和高度

private final int CAMERA_WIDTH = 480;//800;
private final int CAMERA_HEIGHT = 836; //1280;

private Camera mCamera;

//Background variables
private TextureRegion region;

private Scene mScene;

@Override
public EngineOptions onCreateEngineOptions() {
    this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);

    return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}

@Override
protected void onCreateResources() throws IOException {
    BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH+CAMERA_WIDTH,CAMERA_HEIGHT+CAMERA_HEIGHT);

    this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
    atlas.load();

}

@Override
protected Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mScene = new Scene();
    Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());
    this.mScene.setBackground(new SpriteBackground(sprite));

    return this.mScene;
}

我面临的问题是背景图片,它不是全屏幕。我一直在寻找如何在AndEngine中拉伸图像背景,但我没有找到任何好的资源。

这是屏幕截图。

enter image description here

但是图片全屏视图是

enter image description here

请给我一些建议,我应该做些什么,或者为了实现我的目标而采用示例代码。

提前致谢。

3 个答案:

答案 0 :(得分:2)

首先:可爱的背景!第二:您的区域尺寸应为2的幂(256x256,1024x512,2048x2048等)。第三:你的图像尺寸是多少?它们与相机宽度或高度相同吗?如果没有,你可以这样做:

array.each_with_index{ |element,index| element.method(index) }

通常的想法是设置背景的尺寸,而不仅仅是它的位置,因为它没有自动拉伸到屏幕的大小。

答案 1 :(得分:1)

感谢ŁukaszMotyczka,我能够通过制作我的精灵来实现这一目标

float centerX = CAMERA_WIDTH/2;
float centerY = CAMERA_HEIGHT/2;

Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());


这是我的完整代码

private final int CAMERA_WIDTH = 480;
private final int CAMERA_HEIGHT = 836;

private Camera mCamera;
private TextureRegion region;

private Scene mScene;

@Override
public EngineOptions onCreateEngineOptions() {
    this.mCamera = new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);

    return new EngineOptions(true, ScreenOrientation.PORTRAIT_SENSOR, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
}

@Override
protected void onCreateResources() throws IOException {
    BitmapTextureAtlas atlas = new BitmapTextureAtlas(this.getTextureManager(),CAMERA_WIDTH,CAMERA_HEIGHT);
    this.region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas,this,"snow_bg.png",0,0);
    atlas.load();

}

@Override
protected Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mScene = new Scene();
    float centerX = CAMERA_WIDTH/2;
    float centerY = CAMERA_HEIGHT/2;

    Sprite sprite = new Sprite(centerX,centerY,this.region, this.getVertexBufferObjectManager());
    this.mScene.setBackground(new SpriteBackground(sprite));

    return this.mScene;
}

答案 2 :(得分:0)

更改此

Sprite sprite = new Sprite(0,0,region,this.getVertexBufferObjectManager());

Sprite sprite = new Sprite(0, 0, region,getWidth(), region.getHeight(), region, this.getVertexBufferObjectManager());