So I am writing code to animate the player's actor in a side scroller platformer and I have run into a problem. The animate() method I show below works fine as I'm moving to the right; however once I start to go back to the left, the animations cycle between one set of right facing pictures and one set of left facing pictures. Also, when I start to go back to the right after going left, the animations stay messed up and continue to cycle one right facing and one left facing. All the .pngs are drawn with the character facing to the right, so I figured I could simply mirror them instead of creating another set that face left.
Can anyone tell from my code where the error is? I have 13 images that compose the animation, but does anyone have any suggestions for how to control the speed of the function so that I do not need to insert copies of the same picture to make the animation seem slower. Any help is appreciated!
images is a GreenfootImage array and reverse is essentially a copy of images that I created to test if the images in images are being permanently mirrored. Also, this animate() method is in the act() method for the player. It is called after another method that is used to decide if the player should move or not which is used in the scrolling effect.
public void animate(){ if (Greenfoot.isKeyDown("right")){ currentImage = (currentImage + 1) % images.length; setImage(images[currentImage]); } else if (Greenfoot.isKeyDown("left")){ currentImage = (currentImage + 1) % images.length; GreenfootImage[] reverse = images; reverse[currentImage].mirrorHorizontally(); setImage(reverse[currentImage]); } else if (Greenfoot.isKeyDown("left") != true && Greenfoot.isKeyDown("right") != true){ avatar = new GreenfootImage("player0.png"); setImage(avatar); } }