Creating basic image backgrounds with AndEngine

Backgrounds in AndEngine are created by adding Entities to the display stack. As far as I know (no doubt my views will change with new knowledge) there are 2 basic ways to do so. The first is using the SpriteBackground entity and the next using a Sprite. Any of these will create for you a simple flat background with your image of choice. Both are useful, however with the Sprite background I have found no way to control the scaling which seems to be automatic so except you are freezing your application in one orientation mode - Landscape or Portrait there will be squashing or stretching depending on your graphics. Sprites on the other hand by their nature do not change aspect ratio with an orientation change EXCEPT you have added some scaling instructions to the parent in which case the child (the sprite) will inherit its parents behaviour.
The caveat though is to only use the SpriteBackground if you are certain you are going to use a fixed oreintation otherwise all your Sprites will be distorted depending on the orientation of the user

Creating a SpriteBackground/ Sprite as a background
    public void onCreateResources(
            OnCreateResourcesCallback pOnCreateResourcesCallback)
            throws Exception {
        // TODO Auto-generated method stub
        final BuildableBitmapTextureAtlas bitmapTextureAtlas = new BuildableBitmapTextureAtlas(
                mEngine.getTextureManager(), 1024, 1024,

        // Create the texture region
        mTextureRegion = BitmapTextureAtlasTextureRegionFactory
                .createFromAsset(bitmapTextureAtlas, this,
        try {
            // Build/load the bitmap texture atlas
                    .build(new BlackPawnTextureAtlasBuilder(
                            0, 0, 0));
        } catch (TextureAtlasBuilderException e) {


    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {
        // TODO Auto-generated method stub
        mScene = new Scene();
         * Calculate the coordinates for the face, so its centered on the
         * camera.
        final float centerX = (WIDTH - this.mTextureRegion.getWidth()) / 2;
        final float centerY = (HEIGHT - this.mTextureRegion.getHeight()) / 2;

        final Sprite spriteBG = new Sprite(centerX, centerY, this.mTextureRegion,

//      Uncomment these 2 lines if you wish to use a SpriteBackground
//        SpriteBackground bg = new SpriteBackground(spriteBG);
//        mScene.setBackground(bg);

//      Comment this line if you have chosen to use a SpriteBackground


David Evans said...

Searching the internet to find how to add a simple background in AE. Simply copy and pasted your code, worked like charm. Thanks

aYo Binitie said...

I'm glad it's been helpful

My Instagram