i'm rendering an attack animation with the following code:

currentFrame = attackAnimation[3].getKeyFrame(stateTime, false); //3 : left, 2: right
batch.draw(currentFrame, getX(), getY());

This renders the CurrentFrame on the x and y position of the character. This works fine for the right animation as you can see here:

enter image description here

but when i apply the same code for left for example it doesn't draw the character on the correct place and like "pushes himself away" from the bottom left corner and leaving the bounding box.

What am I doing wrong or what am I not seeing?

enter image description here

setGameObject(new Sprite(txtIdle[2]));
setSize(getWidth(), getHeight());
setOrigin(getWidth() /2.0f, getHeight() /2.0f);
setPosition(500, 500);

this code sets an idle image and the size of the bounding box (and the sprite)


It is drawing the bottom-left corner of the texture at the given coordinate (x,y). In the first case, it works because Link's body has an edge to the far left and so he appears inside the box. In the second case, however, the far left side of the texture is the end of the sword, so it places this part in the box and "draws out" from there.

One possible way to fix this would be to render the sword separately from Link's body. The bounding box is just large enough to contain Link, so you could render the sword just outside the bounding box at position (box corner.x + box width, box corner.y) when facing right and at (box corner.x - sword width, box corner.y) when facing the left.

  • \$\begingroup\$ I see, so how do i fix it? \$\endgroup\$ – XverhelstX Jun 21 '14 at 23:48
  • \$\begingroup\$ See my edit for one possible fix. \$\endgroup\$ – Ryan Jun 22 '14 at 0:00
  • \$\begingroup\$ isn't there a way to draw from the origin center of the bounding box? \$\endgroup\$ – XverhelstX Jun 22 '14 at 0:17
  • \$\begingroup\$ I'm not sure exactly what you're asking, sorry. \$\endgroup\$ – Ryan Jun 22 '14 at 0:42
  • 1
    \$\begingroup\$ You could render from the center of the texture by a simple translation of the rendering point by (-texture.width/2, texture.height/2) (assuming +y is down.) The problem is that Link's center, in terms of texture coordinates, is different between the left- and right-facing textures. You can see this more easily if you place one of the textures above the other. \$\endgroup\$ – Ryan Jun 26 '14 at 5:59

You could try and set the bounding box size depending on the sprite size. If for some reason your sprites(animation frames) have different sizes, then use the largest one's size.


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.