user2540406
user2540406

Reputation: 89

libGdx - image background

I have problem with background in libGdx. I try solve mine problem with this:

"In your create() method, create a new Texture referencing your image.png, and then use your existing SpriteBatch to render it in the render() loop. Immediately after your GL.clear() call, go your batch.draw(backgroundTexture, 0. 0) and make sure you're in OrthographicProjection mode for your camera."

I do this:

    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
            
        batch.draw(image, 250, 200);
        batch.draw(backGroundImage, 0, 0);
            
        batch.end();

"image" is mine normal Texture (it's simple image), "backGround" is ofc. mine background. I have "backGround" apply in mine create() method. Mine camer is on camera = new OrthographicCamera();. Here is what I see: ... I need 10 points of reputation to add image :<... Image is to short and cut on lef and right... What I doing wrong. On this solution here, Shinzul said something about loop in in render(), maybe this is mine problem but I dont know how to fix this.

Upvotes: 3

Views: 31124

Answers (4)

H.Sirgani
H.Sirgani

Reputation: 3

I don't know if this is the best way to do it but it worked for me.

sprite.scale(2);
sprite.setCenter(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2);

You can change the scale in (int) until it covers the whole screen.

Upvotes: 0

Mohit Suthar
Mohit Suthar

Reputation: 9375

Simply you can set device hight and width using Gdx, its help me

 batch.draw(mTextureBg, 0 , 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

Upvotes: 3

morpheus05
morpheus05

Reputation: 4872

I think you should draw your backgroung image first.

Besides I think you should look at some tutorials, e.g. https://github.com/libgdx/libgdx/wiki/Spritebatch%2C-Textureregions%2C-and-Sprites. It explains same basic concepts of libgdx.

Upvotes: 11

ajcarracedo
ajcarracedo

Reputation: 265

    public static Texture backgroundTexture;
    public static Sprite backgroundSprite;
    private SpriteBatch spriteBatch;

    private void loadTextures() {
        backgroundTexture = new Texture("images/background.png");
        backgroundSprite =new Sprite(backgroundTexture);
        .
        .
    }

    public void renderBackground() {
        backgroundSprite.draw(spriteBatch);
    }

    public void render() {
        spriteBatch.begin();
            renderBackground(); //In first place!!!!
            drawStuff();
            drawMoreStuff();
            drawMoreMoreStuff();
        spriteBatch.end();
    }

I found this online resource and helped me get started in the world of libgdx. I hope I've been helpful.

Upvotes: 8

Related Questions