Gotcha of Rendering libgdx NinePatch

Sept. 15, 2015, 1:22 p.m. Gamedev Programming libgdx

Today I ran into a gotcha of libgdx. I was trying to render a NinePatch packed with texture packer by constructing a NinePatch using this:

new NinePatch(assetManaget.get("pack.atlas", TextureAtlas.class).findRegion("path/to/ninepatch"));

Then I tried to render the object using its draw() method. Didn't work. Instead, the NinePatch image was just stretched.

Then I read the documentation about the constructor of new NinePatch(TextureRegion)

Construct a degenerate "nine" patch with only a center component.

What? This is obviously not what I was expecting.

Turned out that the proper way to do this is:

assetManaget.get("pack.atlas", TextureAtlas.class).createPatch("path/to/ninepatch");

See also: TextureAtlas.createPatch(java.lang.String)


Comments

Thankful
Oct. 21, 2024, 7:06 a.m.

Thank you! This saved me lots of grief!

View all comments