1
\$\begingroup\$

I'm trying to draw an image using libgdx but no matter what I do, libgdx seems to insist on drawing a portion of an image I had loaded and used in an earlier game screen.

In my current game screen (class implements Screen), I have tried loading the image using both an atlas and loading it via the Texture class. However, when I draw the image in my render method, portions of an image that I had loaded previously are being displayed instead. Not the image that I am passing to the draw method!

I have tried both loading the image using a Texture Atlas and directly:

private TextureAtlas deckAtlas;
private TextureRegion aceHearts;
// In class constructor
deckAtlas = new TextureAtlas("deck.atlas");
aceHearts = deckAtlas.findRegion("ace-heart");

and:

Texture texture = new Texture("deck.png");
sprite = new Sprite(texture, 72, 0, 72, 100);

Depending on the method used above, I try to display by either:

game.batch.begin();
game.batch.draw(aceHearts, 100, 100);
game.batch.end();

Or:

game.batch.begin();
sprite.draw(game.batch);
game.batch.end();

where game is an instance of a class that extends Game and batch was initialized in that class's create method.


To start from the beginning, in my class that extends Game I call

setScreen(new MainMenu(this));

In Main Menu Screen, I load a skin using

skin = new Skin(Gdx.files.internal("uiskin.json"));

and create a bunch of Scene.2d UI widgets with that skin (the packed image that's loaded by the skin is contributing the image that is being drawn on my next screen when I don't want it to). Finally, I create the Screen in which I am having the above problems using

game.setScreen(new TableGame(game));

So to summarize, I am loading an image in a class that implements Screen, but that image never gets drawn to the screen. Instead, an image that I had used in the previous screen is being displayed instead. Any ideas why?

Thanks for any help.

I am using libGDX 1.3.1 (I think).

\$\endgroup\$
-1
\$\begingroup\$

I think your code looks just fine, but the only thing that bothers me, is that you use the batch from Game class.

I think you should use new SpriteBatch for each new Screen and not using the one from Game class.

Or change your structure of code and have Game class which calls render function from LibGDX and from that render function call render functions from Screen class:

Game class:

//defining sprite batch and Screen object
private SpriteBatch batch;
private Screen screen = new Screen();
//RENDER FUNCTION
@Override
public void render(){
    screen.update(Gdx.graphics.getDeltaTime()); // CALLING THE UPDATE FUNCTION FROM SCREEN CLASS AND PASSING IN THE DELTA TIME
    screen.render(batch); // CALLING THE RENDER FUNCTION FROM SCREEN AND PASSING IN THE SPRITEBATCH
}

Screen class:

// defining the image
private Texture texture = new Texture("YOUR PATH");
private Sprite sprite = new Sprite(texture);

//RENDER FUNCTION
public void render(SpriteBatch batch){
    batch.begin();
    sprite.draw(batch,X,Y); 
    batch.end();
}

//UPDATE FUNCTION
public void update(float deltaTime){
    // update stuff here
}

That's it! I hope that I was helpful and I strongly recommend you using the second way because the whole structure is much more organized. SORRY FOR BAD ENGLISH THOUGH!

\$\endgroup\$
  • 1
    \$\begingroup\$ You SHOULD NOT create a new SpriteBatch for each Screen class, just pass it from your main .class that implements Game \$\endgroup\$ – Ion Farima Feb 1 '16 at 6:52
  • \$\begingroup\$ Don't you ever create more tn 1 SpriteBatch! Memory == dead, loading time == dead, efficiency == dead \$\endgroup\$ – Jacob Nov 6 '16 at 14:59
  • \$\begingroup\$ Yeah you are totally right. Was a noob back then... \$\endgroup\$ – captainbuzz123 Nov 6 '16 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.