Parallexing背景颠倒而不是全屏

时间:2014-05-12 01:53:20

标签: android andengine

我正在和Andengine一起制作动态壁纸,我很难开始。我似乎无法获得一个有效的背景。我的代码的最终结果会导致屏幕右上角的资产倒置。

public class WallpaperService extends BaseLiveWallpaperService {

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

private static final String BG_FOREGROUND = "background/arctic_foreground.png";
private static final String BG_MIDGROUND  = "background/arctic_midground.png";
private static final String BG_BACKGROUND = "background/arctic_land.png";

private static final int BG_TEXTURE_ATLAS_HEIGHT_WIDTH = 2048;
private static final int BG_FOREGROUND_START_HEIGHT = 0;
private static final int BG_MIDGROUND_START_HEIGHT = 600;
private static final int BG_BACKGROUND_START_HEIGHT = 1200;

private Camera camera;
private BitmapTextureAtlas backgroundTexture;
private TextureManager textureManager;
private TextureRegion mParallaxLayerFront;
private TextureRegion mParallaxLayerBack;
private TextureRegion mParallaxLayerMid;

@Override
public EngineOptions onCreateEngineOptions() {
    camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new FillResolutionPolicy(), this.camera);
    engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
    textureManager = new TextureManager();
    return engineOptions;
}

@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.backgroundTexture = new BitmapTextureAtlas(textureManager, BG_TEXTURE_ATLAS_HEIGHT_WIDTH, BG_TEXTURE_ATLAS_HEIGHT_WIDTH, TextureOptions.DEFAULT);
    this.mParallaxLayerFront = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_FOREGROUND, 0, BG_FOREGROUND_START_HEIGHT);
    this.mParallaxLayerBack = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_BACKGROUND, 0, BG_MIDGROUND_START_HEIGHT);
    this.mParallaxLayerMid = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.backgroundTexture, this, BG_MIDGROUND, 0, BG_BACKGROUND_START_HEIGHT);

    this.mEngine.getTextureManager().loadTexture(this.backgroundTexture);
}

@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
    final Scene scene = new Scene();
    final AutoParallaxBackground autoParallaxBackground = new AutoParallaxBackground(0, 0, 0, 0);
    autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
            new Sprite(0, 0, this.mParallaxLayerBack, getVertexBufferObjectManager())));

    autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
            new Sprite(0, 0, this.mParallaxLayerMid, getVertexBufferObjectManager())));

    autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
            new Sprite(0, 0, this.mParallaxLayerFront, getVertexBufferObjectManager())));

    scene.setBackground(autoParallaxBackground);
    pOnCreateSceneCallback.onCreateSceneFinished(scene);
}

@Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
    pOnPopulateSceneCallback.onPopulateSceneFinished();
}

Result

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

所有图像都有600像素的高度?问题似乎是图像互相折叠。

问题就在这里:

autoParallaxBackground.attachParallaxEntity(
    new ParallaxBackground.ParallaxEntity(0.0f,
        new Sprite(0, 0, this.mParallaxLayerBack, getVertexBufferObjectManager())));

尝试使用此代码:

autoParallaxBackground.attachParallaxEntity(new ParallaxBackground.ParallaxEntity(0.0f,
     new Sprite(0, CAMERA_HEIGHT- this.mParallaxLayerBack.getHeight(),
                this.mParallaxLayerBack, getVertexBufferObjectManager())));

我希望这会对你有帮助!