3
\$\begingroup\$

I have a sprite that acts as a wall in a game that I am building. I would like to have the brick texture repeat itself instead of stretch to fit the height of the screen. I tried:

Texture lSideTexture = new Texture(Gdx.files.internal("wall.png"));
lSideTexture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);

lSideSprite = new Sprite(lSideTexture);
lSideSprite.setPosition(-50, -100 * (height/width) / 2);
lSideSprite.setSize(5,100 * (height/width));

But I am still getting a texture that has been stretched to fit the dimensions rather than repeated.

Any ideas?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Late answer but this might help.

LibGDX needs you to set the wrap on the Texture like you did, but then, you need to make a TextureRegion of the wrapped Texture and call setRegion(int x, int y, int width, int height) on the created TextureRegion. Then it's the setRegion width and height parameters that will allow your sprite to draw the repeated texture with the specified width and height.

Something like this

Texture textureToRepeat = new Texture(Gdx.files.internal(YOURTEXTUREPATH);
textureToRepeat.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

TextureRegion repeatedTexture = new TextureRegion(textureToReapeat);
repeatedTexture.setRegion(0, 0, WIDTHAFTERREPEAT, HEIGHTAFTERREPEAT);

Sprite sprite = new Sprite(repeatedTexture);

And it should do the trick.

Hope it helps

Disclaimer: this code has not been tested

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .