# How do I cycle through a BufferedImage array of images and display them?

So, I'm coming from Python and Pygame here, porting most of my code because I didn't like the performance. A lot of the syntax is similar, but I've been having some trouble with a few things, namely this animation function.

I'm attempting to cycle through an array of images and have them display an animation. Here's my code so far:

//Animation images.


Then my actual animate function (which I suspect is where the issue is).

public Image animate() {
Image animation = idling[0];
int current_image = 0;
int current_image_add = 1;
if (xa == 0 && ya == 0) {
if (current_image >= 5)
if (current_image <= 0)
if (current_image >= 6)
current_image = 0;
animation = idling[current_image];
}
if (xa == game.speed) {
if (current_image >= 7)
if (current_image <= 0)
animation = walkingRight[current_image];
}
if (xa == -game.speed) {
if (current_image >= 7)
if (current_image <= 0)
animation = walkingLeft[current_image];
}

return animation;
}


While I was looking over this class, I actually noticed that I had no curly brackets surrounding my if statements, and that might be causing problems. Python doesn't require them, so I forgot that Java did. So, I answered my first question before I even asked it.

The draw function, simple as it is.

public void paint(Graphics2D g) {
animate();
g.drawImage(animate(), x, y, null);
}


Now that it will actually swap BufferedImage arrays when I change directions, how do I get it to cycle through the images? Apparently my if statement functions will only swap to the first image and not change to the next ones.

Edit: Alright, I figured that adding some clarification on what this does might help. The arrays contain all of the images for the animations. The animate() function sets the current_image variable to the first index of the array, which is the start of the animation. xa and ya are modified by a move() function elsewhere in the class, which the animate() function checks the value of to determine if the character is moving or not. If xa and ya are zero, the character isn't moving and the idling[] array is chosen. Likewise, if xa is positive or negative game.speed, right or left is chosen, respectively.

The point of this function is to cycle through the indexes by returning Image animation as whatever array and index of that array should be running at the time based on movement. At the end of each if block, it sets animation equal to whatever animation set and the current_image variable to choose the image and index.

• Braces aren't required around single statement if-blocks. So this would still be fine: if (current_image >= 5) current_image_add = -1; – jzx Jul 31 '15 at 22:08

It's unfortunate that I have to answer my own question, but I'll post the fix that I worked out in case it helps someone else.

int current_image = 0;
int current_image_add = 1;


These two variables inside of the animate() function were being reset every time animation() was called, meaning that it never really cycled through anything because it continued to reset to the first frame. Declaring them as global variables instead fixed the problem, like so:

public class Player {
private int current_image = 0;
private int current_image_add = 1;
//snip
}


This revealed a problem with the cycling going through too fast, but I fixed that as well by adding a timer function. Will not go into that since it doesn't help with the original question.