AndEngine background image
Asked Answered
C

3

6

recently I started working on AndEngine. Hard to find up-to-date documentation / helping material in this regard. I am trying to set a background image after going through examples & source code. But for some reason the screen remains blank. I am unable to find any useful info related to this. Here is the code:

public class AndEngineActivity extends BaseGameActivity {

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

    private Camera mCamera;
    private TextureRegion mBgTexture;
    private BitmapTextureAtlas mBackgroundTexture;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public Engine onLoadEngine() {
        // TODO Auto-generated method stub
        this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
    }

    @Override
    public void onLoadResources() {
        // TODO Auto-generated method stub
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

        this.mBackgroundTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.DEFAULT);
        mBgTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackgroundTexture, this, "background.png", 0, 0);

        this.mEngine.getTextureManager().loadTextures(this.mBackgroundTexture);

    }

    @Override
    public Scene onLoadScene() {

        this.mEngine.registerUpdateHandler(new FPSLogger());

        final Scene scene = new Scene();
        final int centerX = (CAMERA_WIDTH -
                mBgTexture.getWidth()) / 2; final int centerY = (CAMERA_HEIGHT -
                mBgTexture.getHeight()) / 2;
        SpriteBackground bg = new SpriteBackground(new Sprite(centerX, centerY, mBgTexture));
        scene.setBackground(bg);

        return scene;
    }

    @Override
    public void onLoadComplete() {
        // TODO Auto-generated method stub

    }
}
Contorted answered 13/3, 2012 at 7:30 Comment(0)
E
5

you need delete :

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
Eight answered 14/3, 2012 at 13:22 Comment(0)
U
-1

Use the following:

CCSprite background=CCSprite.sprite("car7m.jpg" );

Unlettered answered 12/5, 2015 at 13:55 Comment(0)
B
-3

Instead of

    SpriteBackground bg = new SpriteBackground( ... );
    scene.setBackground(bg);

try

    Sprite bg = new Sprite(new Sprite(centerX, centerY, mBgTexture));
    scene.attachChild(bg);
Bolster answered 13/3, 2012 at 7:42 Comment(4)
Hi, i appreciate the response but I tried that too and it didn't help. Am I missing something? Anymore ideas?Contorted
Good news finally got it. as can be seen in the attached code above the onCreate(..) method is overwriting the layout. i removed the method and it's working now.Contorted
Well, yes, you don't want to have that :-)Bolster
He forgot to remove the setContent function call which overrides the surface viewBroach

© 2022 - 2024 — McMap. All rights reserved.