Hi,
I am creating a very simple game where the player moves left and right, avoiding falling objects while collecting gems to increase their score. This is all fine and dandy and easily done, since it is similar to the “simple game” on the LibGDX wiki.
However I am using this little dude called jim, you can fine him here: http://opengameart.org/content/jim
So basically what I am doing is splitting them all up using Gimp, then recreate it using TexturePacker so I can use the TextureAtlas functions.
I am creating the TextureAtlas then using the following code to insert them into the array and size then:
spriteSheet = new TextureAtlas(
Gdx.files.internal("data/img/jimAnim.atlas.txt"));
jimSprites = new Array<Sprite>();
jimSprites = spriteSheet.createSprites();
for (int i = 0; i < jimSprites.size; i++) {
jimSprites.get(i).setSize(32, 90);
}
I then use this code to render my frames:
stateTime += Gdx.graphics.getDeltaTime();
if (Gdx.input.isKeyPressed(Keys.D)) {
while (stateTime > ANIM_DURATION) {
stateTime -= ANIM_DURATION;
currentFrame = (currentFrame == jimSprites.size -1) ? 0
: ++currentFrame;
}
}
Now the problems I am having:
Expected -
In order to save space I just want to have 1 spritesheet, then flip it if needed. So if the player presses A to move left it flips and remains flipped.
Outcome -
Whenever I press A it flips back and forth on each animation end
Code used -
if (Gdx.input.isKeyPressed(Keys.A)) {
jimSprites.get(currentFrame).flip(true, false);
while (stateTime > ANIM_DURATION) {
stateTime -= ANIM_DURATION;
currentFrame = (currentFrame == jimSprites.size -1) ? 0
: ++currentFrame;
}
}
Expected -
When the player is not pressing either A or D, the character uses the correct sprite. I want to ignore his idle state within the array when animating and then fetch it when nothing is pressed.
Outcome -
The sprite is included in the spritesheet and therefore gets animated as if its part of the walking animation
What I have tried -
Various fail code to try and start the animation from a different array index, removing it from the spritesheet and instead draw it like a normal sprite whenever nothing is pressed and only draw the animations when a key is pressed. Is this efficient?
Thanks for any help!