0
\$\begingroup\$

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.
private BufferedImage[] idling = {Sprite.loadSprite("1"), Sprite.loadSprite("2"), Sprite.loadSprite("3"), Sprite.loadSprite("4"),
         Sprite.loadSprite("5"), Sprite.loadSprite("6")};
private BufferedImage[] walkingLeft = {Sprite.loadSprite("L1"), Sprite.loadSprite("L2"), Sprite.loadSprite("L3"), Sprite.loadSprite("L4"),
         Sprite.loadSprite("L5"), Sprite.loadSprite("L6"), Sprite.loadSprite("L7"), Sprite.loadSprite("L8")};
private BufferedImage[] walkingRight = {Sprite.loadSprite("R1"), Sprite.loadSprite("R2"), Sprite.loadSprite("R3"), Sprite.loadSprite("R4"),
         Sprite.loadSprite("R5"), Sprite.loadSprite("R6"), Sprite.loadSprite("R7"), Sprite.loadSprite("R8")};

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) 
            current_image_add = -1;
        if (current_image <= 0) 
            current_image_add = 1;
        if (current_image >= 6)
            current_image = 0;
        current_image += current_image_add;
        animation = idling[current_image];
    }
    if (xa == game.speed) {
        if (current_image >= 7)
            current_image_add = -1;
        if (current_image <= 0)
            current_image_add = 1;
        current_image += current_image_add;
        animation = walkingRight[current_image];
    }
    if (xa == -game.speed) {
        if (current_image >= 7)
            current_image_add = -1;
        if (current_image <= 0)
            current_image_add = 1;
        current_image += current_image_add;
        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.

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

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .