2
\$\begingroup\$

Let's say i have a sprite like this: enter image description here

and i want to draw the lower portion of it, only the yellow color.. Initially the sprite has dimensions 10x10, i want it to be drawn 10x3

Do i have to redimension both the sprite and the texture region to mantain proportion between the two?

\$\endgroup\$
  • \$\begingroup\$ It sounds like you're looking for masking. \$\endgroup\$ – Anko Apr 16 '15 at 20:53
  • \$\begingroup\$ My intent is to draw the life bar of a character. I have the complete bar sprite and i want to draw the portion related to the "health" of the character. So, if i have 30% health, i want th sprite to be drawn 30% tall but if i use the sprite.setSize method, it stretches the texture. Can you explain how do i use masking? \$\endgroup\$ – Catta_ Apr 16 '15 at 21:05
  • \$\begingroup\$ Sorry, I don't know the specifics in Libdgx well enough to explain them myself. I've written an answer explaining the general concept of clipping/masking though, which might help with conceptual understanding. \$\endgroup\$ – Anko Apr 16 '15 at 21:12
  • \$\begingroup\$ A similar question to this was asked recently: gamedev.stackexchange.com/questions/98470/… \$\endgroup\$ – Kurley Apr 16 '15 at 21:13
  • \$\begingroup\$ I found your answer on the other question very useful BUT i have the problem that the sprite was created as a textureregion of an imageAtlas so with frame.getTexture() i get all the atlas and not only the region realted to that sprite!! \$\endgroup\$ – Catta_ Apr 16 '15 at 21:24
0
\$\begingroup\$

The answer I gave in the other question doesn't quite do exactly what you want, but the following example might help. You still need to re-proportion both the heights to do what you want, but you also need to move the input data region down to compensate for the smaller window:

spriteBatch.draw(
    sprite.getTexture(),
    x,
    y,
    width / 2,
    height / 2,
    width,
    height * 0.3f,                                        /* Scale output height */
    sprite.getScaleX(),
    sprite.getScaleY(),
    0,
    sprite.getRegionX(),
    sprite.getRegionY() + sprite.getRegionWidth() * 0.7f, /* Move input data position */
    sprite.getRegionWidth(),
    (int)((float)sprite.getRegionWidth() * 0.3f),         /* Scale input height */
    false,
    false);
\$\endgroup\$

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.