1
\$\begingroup\$

I have a character which has a size of 32x64 when walking or standing. Using the libgdx animation class I can easily animate him with a texture atlas. When I want to make him attack however, the Sprite appears squished because the frames for the attack animation are 64x64 to accommodate the sword. My question is as the artist and programmer, would it make more sense to have each Sprite frame for every animation be 64x64? Or in code, resize the Sprite origin and bounds before the attack animation and set them back to 32x64 when the attack is done?

\$\endgroup\$
  • \$\begingroup\$ For simplicity I would make everything 64x64 and only go the specific way if memory or performance becomes an issue. \$\endgroup\$ – lozzajp Apr 26 '17 at 13:31
0
\$\begingroup\$

It's not wise to increase the size of the smaller sprites, so they fit your needs. If you have a choice, to write more code and gain efficiency - do so.

It's good that these sprites are POT because it saves you alot of memory - keep it that way.

The thing you need to do is to translate your player (and possibly other entities) positions to their center.

For now, if you draw a texture on (0,0), you'd have it fully visible, in the left corner of the screen.

What you need to do is to translate it, so if the width of the player is 64, you draw him at (-32,0). You'd need to adjust some algorithms for it, but it's not complicated. In GDX, you should probably override the draw() method.

If you always draw your entities relative to their width-center, you can use as many animations as you want.

If you have the player's width and height, in your draw() method you can do

batch.draw(currentTexture,
     center.x - (currentTexture.width / 2f),
     center.y - (currentTexture.height / 2f),
     currentTexture.width,
     currentTexture.height);

Each texture will be rendered in the center of the player. You just have to adjust these two variables some reasonable values.

You can switch textures depending on the current state (jump, fight, whatever) - it will always work and you don't have to change the origins (you shouldn't).

\$\endgroup\$
  • \$\begingroup\$ Libgdx has a Sprite origin that you set when creating the Sprite. Mine are always set to width and height divided by two and I draw from that origin. The problem is changing the bounds and origin on the fly when changing an animation \$\endgroup\$ – Joey bobaganoosh Apr 26 '17 at 13:46
  • \$\begingroup\$ Edited, check it out \$\endgroup\$ – Jacob Apr 26 '17 at 14:15
  • \$\begingroup\$ I just tried a bunch of things and I don't think it has anything to do with the origin of the Sprite. I think it's because when I'm setting my bounds at player creation to 32x64, it causes the 64 width Sprite to be squished. But if I change the bounds before I attack, the attack animation shows nothing. Hmm \$\endgroup\$ – Joey bobaganoosh Apr 26 '17 at 20:28

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.