0
\$\begingroup\$

I am using scene2d in libGDX on Android as part of a game I am working on. I have a simple splash screen, which I am trying to display a single image on. The image comes from a file with two tiles in it, and I am using TextureRegion.split() to access it. My problem is, the entire image gets displayed instead of just the first tile.

Here are code snippets to illustrate what I am doing. You should be able to put this in a basic libGDX app to test it out. The image I am using is a png with dimensions of 460 width X 342, (2 tiles of 460X171). I am using the latest libGDX, as obtained from the libGDX setup tool.

What is wrong with my code?

TextureRegion[][] tiles = 
    new TextureRegion(
        new Texture( Gdx.files.internal( "foo.png" ) ) )
            .split( 460, 171 );

Stage stage;
Table table;

stage = new Stage();
table = new Table();

table.setFillParent(true);
stage.addActor(table);

Texture fooTex = tiles[ 0 ][ 0 ].getTexture();

Image fooImg = new Image( fooTex );
fooImg.setPosition(
    ( Gdx.graphics.getWidth() / 2 ) - ( fooImg.getWidth() / 2 ),
    ( Gdx.graphics.getHeight() / 2 ) - ( fooImg.getHeight() / 2 ) );

stage.addActor( fooImg );
\$\endgroup\$
1
\$\begingroup\$

TextureRegion#getTexture() returns the original texture; i.e. the texture that it was split from. Use the actual TextureRegion (in your case, tiles[0][0], not tiles[0][0].getTexture()). Your code should then become this:

TextureRegion fooTex = tiles[0][0];
Image fooImg = new Image(fooTex);

You can also use a static method to split your texture instead of creating a new texture region to do it for you:

TextureRegion[][] tiles = TextureRegion.split(new Texture(Gdx.files.internal("foo.png")), 460, 171);
\$\endgroup\$
  • \$\begingroup\$ Thanks! That worked perfectly. Guess I should have looked closer at the API. \$\endgroup\$ – MukRaker May 1 '17 at 14:05

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.