0

Am i implementing this correctly? I'm having issues with the direction my character is in, yet the state manager is working correctly. I'm unsure whether this is the problem. I have my player facing right automatically when constructed yet he faces left.

I have two arrays for both right and left animation inside my assets manager, The original images are facing right, i declare that array and then flip the same image on the left array. Does the left array override the right?

int idleRIndex = 1;
            TextureRegion[] idleRight = new TextureRegion[3];
            for (int i = 0; i < idleRight.length; i++)
            {
                idleRight[i] = rubenSprite.findRegion("iframe" + idleRIndex);
                idleRIndex++;
            }
            rubenIdleRight = new Animation(0.2f, idleRight); 

int idleLIndex = 1;
                TextureRegion[] idleLeft = new TextureRegion[3];
                for (int i = 0; i < idleLeft.length; i++)
                {
                    idleLeft[i] = rubenSprite.findRegion("iframe" + idleLIndex);
                    idleLeft[i].flip(true, false);
                    idleLIndex++;
                }
                rubenIdleLeft = new Animation(0.2f, idleLeft);
BradleyIW
  • 1,338
  • 2
  • 20
  • 37

1 Answers1

2

It seems so after a test. The findRegion("iframe" + idleLIndex) and for the right are returning the same object reference. So I think the flip for the left will also affect the right. Maybe you can create a new TextureRegion object from the atlas. It shouldn't be much overhead. Try:

idleLeft[i] = new TextureRegion(rubenSprite.findRegion("iframe" + idleLIndex));

Then the flip shouldn't affect the right anymore.

Quallenmann
  • 323
  • 2
  • 8
  • @Quallenmann Which one is more better? Flip "on the fly" when drawing or prepare flipped TextureRegion like your answer which mean is duplicate it. – Kenjiro May 21 '15 at 19:33
  • If you mean by "on the fly" something like that. [SpriteBatch.draw](http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/SpriteBatch.html#draw-com.badlogic.gdx.graphics.Texture-float-float-float-float-int-int-int-int-boolean-boolean-) where you can flip the image with some boolean, then it would be better if you do it "on the fly". Cause if the flip booleans are set, this function only change the texturecoordinates (if flippedX the u1 and u2 are swapped, if flippedY the v1 and v2) which isn't that much computation, and the memory which will be used are lesser. – Quallenmann May 21 '15 at 20:40