Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 417
user2540406 Avatar asked Jun 26 '26 07:06

user2540406


2 Answers

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.

like image 103
morpheus05 Avatar answered Jun 27 '26 19:06

morpheus05


    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.

like image 41
ajcarracedo Avatar answered Jun 27 '26 19:06

ajcarracedo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!