I have a character that has 2 animations both when idle and when walking. Whenever i press the left or right key the image is flipped in the direction i pressed. However the problem is that when implementing this with the idle animation it flickers yust a bit but fast, but the code works fine only the flickering part right after a flip for a split second is the problem.
this is the code:
elapsedTime += Gdx.graphics.getDeltaTime();
Array<TextureAtlas.AtlasRegion> frames = textureAtlas.getRegions();
animation = new Animation(1f/10f, textureAtlas.getRegions());
for(TextureRegion frame: frames){
if(body.getLinearVelocity().x > 0 && !frame.isFlipX()){
frame.flip(true, false);
} else if (body.getLinearVelocity().x < 0 && frame.isFlipX()){
frame.flip(true, false);
}
}
It could be that you're seeing one frame of it facing the other way when it first comes to rest from walking the opposite direction from how you were facing the last time you were idle. You need to track which way the character is facing in a variable so whenever the velocity drops to zero, you still know which way to make it face.
An alternative solution that doesn't involve repeatedly flipping your animations is to simply draw your texture region flipped when you need to like this: